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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8378563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:27:09  来源:igfitidea点击:

Why can't I change the type of an input element to submit?

jquerytypesinputsubmit

提问by kmb64

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):

我收到此错误(在 Firefox 中):

uncaught exception: type property can't be changed

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

Are there any work arounds?

有什么解决办法吗?

回答by adeneo

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

Be aware that this changes the type but not the value, so the button will still read "confirm".

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

A fiddle to prove it : http://jsfiddle.net/qeUxP/

证明它的小提琴:http: //jsfiddle.net/qeUxP/

回答by J. Bruni

Why jQuery won't allow you to just change the type attribute?

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

Because it causes problems in Internet Explorer.

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

More info at: change type of input field with jQuery

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

"Straight from the jQuery source":

“直接来自 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";

回答by Basem Sayej

It worked finally , try finding the following:

它终于奏效了,尝试找到以下内容:

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

回答by Candide

Here's a workaround:

这是一个解决方法:

$('<input name="confirm_button" id="confirm_button2" type="submit" value="Confirm" class="login_btn" />').insertAfter('#confirm_button');
$('#confirm_button').remove();

回答by davidethell

Why not just bind to the click event and call the form submit in your click handler?

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

回答by saidesh kilaru

javascript:

javascript:

function submit_button(){

      var btn=document.getElementById('button_id');
      btn.setAttribute('type', 'submit');

  }

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

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

Jquery:

查询:

function submit_button(){

 $('#' + button_id).prop('type', 'submit');

   }

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

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

回答by Rich O'Kelly

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>元素可以用于提交表单、呈现为按钮,并且不需要任何 javascript 来执行此操作,例如:

<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');