jQuery 添加必需的输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19166685/
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
jQuery add required to input fields
提问by Miura-shi
I have been searching ways to have jQuery automatically write required using html5 validation to my all of my input fields but I am having trouble telling it where to write it.
我一直在寻找方法让 jQuery 使用 html5 验证自动将所需的内容写入我的所有输入字段,但我无法告诉它在哪里编写它。
I want to take this
我想拿这个
<input type="text" name="first_name" value="" id="freeform_first_name"
maxlength="150">
and have it automatically add requiredbefore the closing tag
并让它在结束标记之前自动添加required
<input type="text" name="first_name" value="" id="freeform_first_name"
maxlength="150" required>
I thought I could do someting along the lines of
我以为我可以做一些类似的事情
$("input").attr("required", "true");
But it doesn't work. Any help is greatly appreciated.
但它不起作用。任何帮助是极大的赞赏。
回答by Unknown
回答by Joz Naveen Joz
You can do it by using attr, the mistake that you made is that you put the true inside quotes. instead of that try this:
您可以通过使用 attr 来做到这一点,您犯的错误是将 true 放在引号内。而不是尝试这个:
$("input").attr("required", true);
回答by John Meyer
I have found that the following implementations are effective:
我发现以下实现是有效的:
$('#freeform_first_name').removeAttr('required');
$('#freeform_first_name').attr('required', 'required');
These commands (attr, removeAttr, prop) behave differently depending on the version of JQuery you are using. Please reference the documentation here: https://api.jquery.com/attr/
这些命令(attr、removeAttr、prop)的行为取决于您使用的 JQuery 版本。请参考此处的文档:https: //api.jquery.com/attr/
回答by Dexter
Using .attr
method
使用.attr
方法
.attr(attribute,value); // syntax
.attr("required", true);
// required="required"
.attr("required", false);
//
Using .prop
使用 .prop
.prop(property,value) // syntax
.prop("required", true);
// required=""
.prop("required", false);
//
Read more from here
从这里阅读更多
回答by Rokan Nashp
Should not enclose truewith double quote" " it should be like
如果不附上真实与双引号“”它应该像
$(document).ready(function() {
$('input').attr('required', true);
});
Also you can use prop
你也可以使用道具
jQuery(document).ready(function() {
$('input').prop('required', true);
});
Instead of trueyou can try required. Such as
您可以尝试 required而不是true。如
$('input').prop('required', 'required');
回答by Doris Gammenthaler
I found that jquery 1.11.1 does not do this reliably.
我发现 jquery 1.11.1 不能可靠地做到这一点。
I used $('#estimate').attr('required', true)
and $('#estimate').removeAttr('required')
.
我用过$('#estimate').attr('required', true)
和$('#estimate').removeAttr('required')
。
Removing required
was not reliable. It would sometimes leave the required
attribute without value. Since required
is a boolean attibute, its mere presence, without value, is seen by the browser as true
.
删除required
是不可靠的。它有时会使required
属性没有值。由于required
是一个布尔属性,它的存在,没有价值,被浏览器视为true
.
This bug was intermittent, and I got tired of messing with it. Switched to document.getElementById("estimate").required = true
and document.getElementById("estimate").required = false
.
这个错误是间歇性的,我厌倦了把它弄乱。切换到document.getElementById("estimate").required = true
和document.getElementById("estimate").required = false
。