设置名为“required”的属性和任何值,与JQuery,不起作用 [英] Setting an attribute named "required" and any value, with JQuery, doesn't work

查看:1799
本文介绍了设置名为“required”的属性和任何值,与JQuery,不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  $(#elementId)。attr(required,true ); 

在Chrome和Firefox中,DOM都会生成 required 作为属性(无值)或 required =(空值)。

这个例子中的值是否为true无关紧要。如果你尝试asdf,同样的事情发生。

奇怪的是我相信这个使用起作用,因为这个新代码是这是一个持续数年的大型项目。

我唯一能想到的就是我的Chrome(v10)和Firefox(v4)现在已经足够先进了他们将必填属性视为HTML5保留关键字。我添加了 novalidate 属性,认为这可能会关闭任何与表单相关的HTML5内容。没有这样的运气。



想法?



编辑:



为了澄清,这只发生在JQuery中。如果我这样说,它可以工作:

  $(#elementId)[0] .setAttribute(required,真正); 

JQuery中存在一个错误吗?任何想法为什么这只发生在JQuery?我们的开发团队尽可能使用所有代码来通过JQuery。我可以使用直接的setAttribute JavaScript方法,但宁可使用JQuery解决方案。

编辑2:



问题的症结在于......



为什么使用JQuery的 attr() setAttribute()方法执行时,c $ c>方法不起作用? attr()方法调用 setAttribute()在某个点下降吗?



这是如此混乱。如果你使用setAttribute(),那么Chrome和Firefox是完全正确的设置 required =true

解决方案

确实与浏览器有关。我检查了IE8,它将应用您设置的所有字符串值到必需的属性。



由于您不需要任何值(只有属性必须存在),这不会影响默认行为。 )



<要求输入> 是如果您正在滥用javascript钩子的属性值,则是另一回事: < input required => < input required =honky-tonk> $(input)。 .attr(derp,derty),它将它分配给元素。



我的猜测是jquery使用 required =对于那些希望在XHTML严格语法中使用它的人来说,仍然符合HTML5标准。 http://dev.w3.org/html5/spec/Overview .html#the-required-attribute



http://dev.w3.org/html5/spec/Overview.html#boolean-attribute


This doesn't work:

$("#elementId").attr("required", "true");

In both Chrome and Firefox, the DOM produces either required as the attribute (no value) or required="" (empty value).

And it doesn't matter that the value in this example is "true". If you try "asdf" the same thing happens.

What's odd is that I believe this used to work because this new code is part of a large project that's been ongoing for several years.

The only thing I can think of is that my Chrome (v10) and Firefox (v4) are now both sufficiently advanced that they're recognizing the required attribute as an HTML5 reserved keyword. I added the novalidate attribute, thinking that that might turn off any form-related HTML5-ness. No such luck.

Thoughts?

Edit:

To clarify, this only happens with JQuery. If I say this, it works:

$("#elementId")[0].setAttribute("required", "true");

Is there a bug in JQuery? Any idea why this only happens with JQuery? Our development team likes all code to go through JQuery where possible. I can use the straight setAttribute JavaScript method, but would rather use a JQuery solution that works.

Edit 2:

The crux of the matter is this...

Why does using JQuery's attr() method not work when the regular setAttribute() method does? Doesn't the attr() method call setAttribute() at some point lower down?

That is what is so confusing. Chrome and Firefox are perfectly fine setting required="true" if you use setAttribute().

解决方案

It is indeed browser related. I checked in IE8 and it will apply whatever string value you set to the required attribute.

Since you don't need any value (only the attribute must be present), this won't affect default behavior. If you are abusing the attribute value for javascript hooks, that's another thing :)

<input required> is the same as <input required=""> and <input required="honky-tonk">

Since IE8 doesn't support html5, it's just like setting a made-up attribute, so you could set $("input").attr("derp", "derty") and it would assign it to the element.

My guess is that jquery uses the required="" for folks who wish to use this in XHTML strict syntax, while still conforming to HTML5 standards.

http://dev.w3.org/html5/spec/Overview.html#the-required-attribute

http://dev.w3.org/html5/spec/Overview.html#boolean-attribute

这篇关于设置名为“required”的属性和任何值,与JQuery,不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆