Ian*_*oyd 75 javascript validation html5
我试图在Javascript text中根据需要标记输入框.
<input id="edName" type="text" id="name">
Run Code Online (Sandbox Code Playgroud)
如果该字段最初标记为required:
<form>
<input id="edName" type="text" id="name" required><br>
<input type="submit" value="Search">
</form>
Run Code Online (Sandbox Code Playgroud)
当用户尝试提交时,他们会收到验证错误:

但现在我想通过Javascript required在"运行时"设置属性:
<form>
<input id="edName" type="text" id="name"><br>
<input type="submit" value="Search">
</form>
Run Code Online (Sandbox Code Playgroud)
与相应的脚本:
//recommended W3C HTML5 syntax for boolean attributes
document.getElementById("edName").attributes["required"] = "";
Run Code Online (Sandbox Code Playgroud)
除非我现在提交,否则没有验证检查,没有阻止.
设置HTML5验证布尔属性的正确方法是什么?
HTML5验证required属性记录为Boolean:
4.10.7.3.4
required属性该
required属性是布尔属性.指定时,元素是必需的.
关于如何定义boolean属性有很多令人费解的事.HTML5规范说明:
元素上存在布尔属性表示真值,缺少属性表示false值.
如果该属性存在,则其值必须是空字符串,或者是属性的规范名称的ASCII不区分大小写匹配的值,没有前导或尾随空格.
这意味着您可以通过两种不同的方式指定required 布尔属性:
edName.attributes.required = ""; //the empty string
edName.attributes.required = "required"; //the attribute's canonical name
Run Code Online (Sandbox Code Playgroud)
当你查看我的问题的jsFiddle时,你会注意到如果required在标记中定义了属性:
<input id="edName" type="text" id="name" required>
Run Code Online (Sandbox Code Playgroud)
那么属性的值不是空字符串,也不是属性的规范名称:
edName.attributes.required = [object Attr]
Run Code Online (Sandbox Code Playgroud)
这可能会导致解决方案.
T.J*_*der 94
required是反射的属性(如id,name,type,并且这样的),所以:
element.required = true;
Run Code Online (Sandbox Code Playgroud)
... element实际的inputDOM元素在哪里,例如:
document.getElementById("edName").required = true;
Run Code Online (Sandbox Code Playgroud)
(只是为了完整.)
回覆:
那么属性的值不是空字符串,也不是属性的规范名称:
Run Code Online (Sandbox Code Playgroud)edName.attributes.required = [object Attr]
那是因为required代码是属性对象,而不是字符串; attributes是一个NamedNodeMap值为Attr对象的值.为了获得其中一个的价值,你会看看它的value属性.但是对于布尔属性,该值不相关; 属性要么存在于地图中(true),要么不存在(false).
因此,如果required 没有反映,您可以通过添加属性来设置它:
element.setAttribute("required", "");
Run Code Online (Sandbox Code Playgroud)
......相当于element.required = true.你可以通过完全删除它来清除它:
element.removeAttribute("required");
Run Code Online (Sandbox Code Playgroud)
......相当于element.required = false.
但是我们不必这样做required,因为它已被反映出来.
Ian*_*oyd 85
element.setAttribute("required", ""); //turns required on
element.required = true; //turns required on through reflected attribute
jQuery(element).attr('required', ''); //turns required on
$("#elementId").attr('required', ''); //turns required on
element.removeAttribute("required"); //turns required off
element.required = false; //turns required off through reflected attribute
jQuery(element).removeAttr('required'); //turns required off
$("#elementId").removeAttr('required'); //turns required off
if (edName.hasAttribute("required")) { } //check if required
if (edName.required) { } //check if required using reflected attribute
Run Code Online (Sandbox Code Playgroud)
一旦TJ Crowder设法指出反射属性,我就会发现以下语法错误:
element.attributes["name"] = value; //bad! Overwrites the HtmlAttribute object
element.attributes.name = value; //bad! Overwrites the HtmlAttribute object
value = element.attributes.name; //bad! Returns the HtmlAttribute object, not its value
value = element.attributes["name"]; //bad! Returns the HtmlAttribute object, not its value
Run Code Online (Sandbox Code Playgroud)
你必须经历element.getAttribute和element.setAttribute:
element.getAttribute("foo"); //correct
element.setAttribute("foo", "test"); //correct
Run Code Online (Sandbox Code Playgroud)
这是因为该属性实际上包含一个特殊的HtmlAttribute对象:
element.attributes["foo"]; //returns HtmlAttribute object, not the value of the attribute
element.attributes.foo; //returns HtmlAttribute object, not the value of the attribute
Run Code Online (Sandbox Code Playgroud)
通过将属性值设置为"true",您错误地将其设置为String对象,而不是它需要的HtmlAttribute对象:
element.attributes["foo"] = "true"; //error because "true" is not a HtmlAttribute object
element.setAttribute("foo", "true"); //error because "true" is not an HtmlAttribute object
Run Code Online (Sandbox Code Playgroud)
从概念上讲,正确的想法(用打字语言表达)是:
HtmlAttribute attribute = new HtmlAttribute();
attribute.value = "";
element.attributes["required"] = attribute;
Run Code Online (Sandbox Code Playgroud)
这就是为什么:
getAttribute(name)setAttribute(name, value)存在.他们完成了将值分配给内部HtmlAttribute对象的工作.
除此之外,还会反映一些属性.这意味着您可以通过Javascript更好地访问它们:
//Set the required attribute
//element.setAttribute("required", "");
element.required = true;
//Check the attribute
//if (element.getAttribute("required")) {...}
if (element.required) {...}
//Remove the required attribute
//element.removeAttribute("required");
element.required = false;
Run Code Online (Sandbox Code Playgroud)
什么,你不想要做的就是误用.attributes集合:
element.attributes.required = true; //WRONG!
if (element.attributes.required) {...} //WRONG!
element.attributes.required = false; //WRONG!
Run Code Online (Sandbox Code Playgroud)
这导致围绕使用required属性进行测试,比较通过属性返回的值和反射属性
document.getElementById("name").required;
document.getElementById("name").getAttribute("required");
Run Code Online (Sandbox Code Playgroud)
结果:
HTML .required .getAttribute("required")
========================== =============== =========================
<input> false (Boolean) null (Object)
<input required> true (Boolean) "" (String)
<input required=""> true (Boolean) "" (String)
<input required="required"> true (Boolean) "required" (String)
<input required="true"> true (Boolean) "true" (String)
<input required="false"> true (Boolean) "false" (String)
<input required="0"> true (Boolean) "0" (String)
Run Code Online (Sandbox Code Playgroud)
试图.attributes直接访问该集合是错误的.它返回表示DOM属性的对象:
edName.attributes["required"] => [object Attr]
edName.attributes.required => [object Attr]
Run Code Online (Sandbox Code Playgroud)
这就解释了为什么你不应该.attributes直接与收藏品交谈.您不是在操纵属性的值,而是操纵属性本身的对象.
设置required属性的正确方法是什么?您有两个选择,可以是反射属性,也可以通过正确设置属性:
element.setAttribute("required", ""); //Correct
edName.required = true; //Correct
Run Code Online (Sandbox Code Playgroud)
严格地说,任何其他值都将"设置"该属性.但Boolean属性的定义规定只应将其设置为空字符串""以指示true.下面的方法都工作到设定的required 布尔属性,
但不要使用它们:
element.setAttribute("required", "required"); //valid, but not preferred
element.setAttribute("required", "foo"); //works, but silly
element.setAttribute("required", "true"); //Works, but don't do it, because:
element.setAttribute("required", "false"); //also sets required boolean to true
element.setAttribute("required", false); //also sets required boolean to true
element.setAttribute("required", 0); //also sets required boolean to true
Run Code Online (Sandbox Code Playgroud)
我们已经了解到尝试直接设置属性是错误的:
edName.attributes["required"] = true; //wrong
edName.attributes["required"] = ""; //wrong
edName.attributes["required"] = "required"; //wrong
edName.attributes.required = true; //wrong
edName.attributes.required = ""; //wrong
edName.attributes.required = "required"; //wrong
Run Code Online (Sandbox Code Playgroud)
努力当诀窍删除的required属性是很容易不小心打开它:
edName.removeAttribute("required"); //Correct
edName.required = false; //Correct
Run Code Online (Sandbox Code Playgroud)
使用无效方式:
edName.setAttribute("required", null); //WRONG! Actually turns required on!
edName.setAttribute("required", ""); //WRONG! Actually turns required on!
edName.setAttribute("required", "false"); //WRONG! Actually turns required on!
edName.setAttribute("required", false); //WRONG! Actually turns required on!
edName.setAttribute("required", 0); //WRONG! Actually turns required on!
Run Code Online (Sandbox Code Playgroud)
使用反射.required属性时,您还可以使用任何"falsey"值将其关闭,并使用truthy值将其打开.但为了清晰起见,坚持真假.
required?通过以下.hasAttribute("required")方法检查属性是否存在:
if (edName.hasAttribute("required"))
{
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过布尔反射.required属性检查它 :
if (edName.required)
{
}
Run Code Online (Sandbox Code Playgroud)
和jquery版本:
$('input').attr('required', true)
$('input').attr('required', false)
Run Code Online (Sandbox Code Playgroud)
我知道这是不可能的,但也许有人会觉得这很有帮助:)
重要的不是属性而是属性,它的值是布尔值.
您可以使用它进行设置
document.getElementById("edName").required = true;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
169394 次 |
| 最近记录: |