简体   繁体   English

设置输入的“type”属性不适用于jQuery attr()方法

[英]Setting the “type” attribute of an input does not work with jQuery attr() method

I looked into previous questions, but they didn't seem to answer what's happening to me. 我查看过以前的问题,但他们似乎并没有回答我发生的事情。
In my real code i'm creating a form on the fly and adding to it two buttons, one for submission and another for other function. 在我的真实代码中,我正在创建一个表单并添加两个按钮,一个用于提交,另一个用于其他功能。 For this I'm setting the "type" attribute of the buttons to "submit" for one and "button" for the other. 为此,我将按钮的“type”属性设置为“submit”为一个,“按钮”为另一个。 The problem is that in Chrome both buttons submit the form. 问题是在Chrome中,两个按钮都会提交表单。

Code for the form: 表格代码:

form = $(document.createElement('form')).attr('method', 'get').attr('action', defaults.action).appendTo(object);

Code for the buttons: 按钮代码:

form.append(
    $(document.createElement('div')).
        attr('class', classesHash.buttonsContainer).
        append(
            $(document.createElement('button')).
                attr('type', 'submit').
                addClass(classesHash.submitButton).
                attr('title', i18n('Filter')).
                attr('value', i18n('Filter')).
                append(i18n('Filter'))
        ).
        append(
            $(document.createElement('button')).
                attr('type', 'button').
                addClass(classesHash.addButton).
                attr('title', i18n('Add filter')).
                attr('value', i18n('Add filter')).
                append(i18n('Add filter')).
            click(addFilter)
        )
);

I made a more simple test with this HTML code: 我用这个HTML代码做了一个更简单的测试:

<form action="" method="get"><button id="test">test</button></form>

When Chrome doesn't finds a submit button, any button submits the form. 当Chrome找不到提交按钮时,任何按钮都会提交表单。

The following doesn't works, the form gets submitted on button click: 以下不起作用,表单在按钮点击时提交:

$('#test').attr('type', 'button');

The following does works, the form does not submit on button click: 以下工作正常,单击按钮时表单不提交:

document.getElementById('test').setAttribute('type', 'button');

The form and the button are being generated dynamically and I'm using jQuery so attr() is the most obvious method. 表单和按钮是动态生成的,我使用的是jQuery,因此attr()是最明显的方法。 Is something wrong with the jQuery core and Chrome's JS specification? jQuery核心和Chrome的JS规范有问题吗? It works fine in Firefox. 它在Firefox中运行良好。 Thanks a lot. 非常感谢。

First, the correct approach: 一,正确的方法:
To do what you want in this case, go with the vanilla JavaScript solution, but test it in IE! 要在这种情况下做你想做的事,请使用vanilla JavaScript解决方案, 但在IE中进行测试!


The why: 原因:
The reason type doesn't work is because it fails in IE (you can't chagne the type of an input after it's added to the DOM, and it's handled in this same way), so jQuery throws an error when you try . 原因type不起作用是因为它在IE中失败(在添加到DOM之后你不能对输入的type进行处理,并且它以相同的方式处理),所以当你尝试时jQuery会抛出一个错误 It does this specifically for <input> and <button> elements when changing the type attribute . 它在更改type属性时专门用于<input><button>元素

If you look in your console you'll see this: 如果你在控制台中查看,你会看到:

Error: Uncaught type property can't be changed 错误:无法更改未捕获的类型属性

Here's a quick test showing this , check the console to see the error jQuery throws. 这是一个显示此问题的快速测试 ,请检查控制台以查看jQuery抛出的错误。

Use prop instead of attr . 使用prop而不是attr It`ll work for sure. 它肯定会起作用。

Please look at the below sample code: 请看下面的示例代码:

$("input[name='email']").focusin(function() {
           console.log("alert");
            $('#email').prop('type','number');
});

Let me know your thoughts on this. 让我知道你对此的看法。

Thanks 谢谢

So this post was helpful to me, but my solution was to clone the button in question and replace it 所以这篇文章对我有帮助,但我的解决方案是克隆有问题的按钮并替换它

$new = $(this).clone();
$new.attr('type','hidden');
$this_form.append($new);
$(this).remove();

I'm not 100% sure what you're asking, but I think you're asking about preventing the submission of a form with a button click in Chrome. 我不是100%肯定你在问什么,但我认为你要求阻止在Chrome中点击按钮提交表单。 If that is the case, why not use preventDefault ? 如果是这种情况,为什么不使用preventDefault

$("#test").click(function(e) {
  e.preventDefault();
  //more work here....
});

EDIT: 编辑:
I agree with Nick that in order to do what you are trying to do, go with the straight JavaScript approach. 我同意Nick的意见,为了做你想做的事,请采用直接的JavaScript方法。 But in that case, you're applying attributes to a button that don't make sense (or at least aren't valid). 但在这种情况下,您将属性应用于一个没有意义(或至少无效)的按钮。 Since you're already using jQuery, why not handle it properly and prevent the default action of a button click in a form? 由于您已经在使用jQuery,为什么不正确处理它并阻止表单中按钮单击的默认操作?

jQuery对我来说在Chrome中运行良好...我今天抛出的所有功能都运行得很好,包括.attr()......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM