为什么我不能改变要提交的输入元素的类型?

kmb*_*b64 23 jquery types input submit

我正在调用这个函数:

function submit_button(button_id){
    $('#' + button_id).attr('type', 'submit');
}
Run Code Online (Sandbox Code Playgroud)

使这个按钮类型=提交而不是按钮:

<input  name="confirm_button" id="confirm_button" type="button" value="Confirm" class="login_btn" />
Run Code Online (Sandbox Code Playgroud)

我得到这个错误(在Firefox中):

未捕获的异常:类型属性无法更改

有没有工作?

ade*_*neo 43

function submit_button(button_id){
    $('#' + button_id).prop('type', 'submit');
}
Run Code Online (Sandbox Code Playgroud)

请注意,这会更改类型而不是值,因此按钮仍会显示"确认".

一个小提琴来证明它:http://jsfiddle.net/qeUxP/


J. *_*uni 14

为什么jQuery不允许你只改变type属性?

因为它会导致Internet Explorer出现问题.

更多信息:使用jQuery更改输入字段的类型

"直接来自jQuery源代码":

// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
    throw "type property can't be changed";
Run Code Online (Sandbox Code Playgroud)


小智 5

它最终工作,尝试找到以下内容:

   $("#confirm_button").each(function () 
   { this.type = "number"; });
Run Code Online (Sandbox Code Playgroud)