Why can't I change the type of an input elemen

2020-02-26 15:20发布

I'm calling this function:

function submit_button(button_id){
    $('#' + button_id).attr('type', 'submit');
}

to make this button type = submit instead of button.:

<input  name="confirm_button" id="confirm_button" type="button" value="Confirm" class="login_btn" />

and I get this error (in firefox):

uncaught exception: type property can't be changed

Are there any work arounds?

7条回答
淡お忘
2楼-- · 2020-02-26 15:43

The workaround is to create an element of the correct type, and replace the currently exisitng one.

However, in your example, a <button> element could serve the purpose of submitting the form, render as a button, and not require any javascript to do so, eg:

<button name="confirm_button" id="confirm_button" type="input" value="Confirm" class="login_btn" disabled />

And then to make the button enabled again:

$('confirm_button').removeAttr('disabled');
查看更多
登录 后发表回答