简体   繁体   English

输入字段需要jQuery add

[英]jQuery add required to input fields

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 required before the closing tag 并在结束标记之前自动添加所需内容

 <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. 任何帮助是极大的赞赏。

$("input").prop('required',true);

DEMO FIDDLE

You can do it by using attr, the mistake that you made is that you put the true inside quotes. 你可以通过使用attr来做到这一点,你犯的错误是你把真正的内部引号。 instead of that try this: 而不是尝试这个:

$("input").attr("required", true);

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. 这些命令(attr,removeAttr,prop)的行为会有所不同,具体取决于您使用的JQuery版本。 Please reference the documentation here: https://api.jquery.com/attr/ 请在此处参考文档: https//api.jquery.com/attr/

Should not enclose true with 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 true you can try required. 而不是真的,你可以尝试所需。 Such as

$('input').prop('required', 'required');

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 从这里了解更多

https://stackoverflow.com/a/5876747/5413283 https://stackoverflow.com/a/5876747/5413283

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. 删除required不可靠。 It would sometimes leave the required attribute without value. 它有时会保留required属性而没有值。 Since required is a boolean attibute, its mere presence, without value, is seen by the browser as true . 由于required是一个布尔属性,因此浏览器将其视为true存在而无价值。

This bug was intermittent, and I got tired of messing with it. 这个bug是间歇性的,我厌倦了搞乱它。 Switched to document.getElementById("estimate").required = true and document.getElementById("estimate").required = false . 切换到document.getElementById("estimate").required = truedocument.getElementById("estimate").required = false

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

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