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

我正在调用这个函数:

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

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

  

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

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

有没有工作?

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

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

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

为什么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"; 

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

  $("#confirm_button").each(function () { this.type = "number"; }); 

这是一个解决方法:

 $('').insertAfter('#confirm_button'); $('#confirm_button').remove(); 

为什么不绑定到click事件并在单击处理程序中调用表单提交?

JavaScript的:

 function submit_button(){ var btn=document.getElementById('button_id'); btn.setAttribute('type', 'submit'); } 

================================================== ====================

jQuery的:

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

================================================== ====================

解决方法是创建一个正确类型的元素,并替换当前的exititng元素。

但是,在您的示例中, 元素可以用于提交表单,呈现为按钮,而不需要任何javascript来执行此操作,例如:

  

然后再次启用按钮:

 $('confirm_button').removeAttr('disabled');