jQuery 1.9.1属性选择器 [英] jQuery 1.9.1 property selector

查看:110
本文介绍了jQuery 1.9.1属性选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



从jQuery 1.9开始, .attr(..)方法不再返回属性值,我们现在必须使用 .prop(..)。不幸的是,这也适用于通过属性选择器指定的属性,即 $(input [value =]) p

请参阅
http://jquery.com/upgrade-guide /1.9/#attr-versus-prop-



以及关于 .attr .prop





我目前使用的选择器如 $(input [value =]) $( select [value =])
选择没有设置值的输入元素。然而,这不再适用于jQuery 1.9,而是我现在正在做这样的事情:



$ p $ var hasValue = function() {return !!($(this).val()。length); };
var hasNoValue = function(){return!($(this).val()。length); };
$(input)。filter(hasValue);
$(select)。filter(hasValue);

我的实际选择器有点大,检查有或没有值的多个元素,现在我有将我的1选择器字符串拆分为多个选择器,其间使用.filter(..)方法调用。

问题

是否有等价于 $ ([value =]) $([value!=]) $(值='abc'])它使用属性而不是属性?如果没有,是否有比使用 .filter(hasValue) .filter(hasNoValue)方法更简洁的方法?



谢谢

解决方案使用。过滤器似乎是唯一的方法,但它不是太糟糕,你可以通过使用 .val : p>

  $(:input)。filter(function(){return $(this).val()=== ;}); 

如果这确实是您应该受到的谴责,您可以创建自定义选择器。

  $。expr [':']。emptyInput = function(elem){
return $(elem).is(:input )&& $(elem).val()===;
};

http://jsfiddle.net/ExplosionPIlls/zaZPp/



编辑:您也可以使用 this.value 而不是 $(elem).val()


Background

As of jQuery 1.9 the .attr(..) method no longer returns property values, instead we now have to use .prop(..). Unfortunately this also applies to attributes specified via an attributes selector i.e. $("input[value=]")

See http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

and a good SO discussion on the differences between .attr and .prop :

.prop() vs .attr()

My Situation

I'm currently using selectors like $("input[value=]") and $("select[value=]") to select input elements that have no value set. However, this no longer works with jQuery 1.9, instead I'm now doing something like this:

var hasValue = function () { return !!($(this).val().length); };
var hasNoValue = function () { return !($(this).val().length); };
$("input").filter(hasValue);
$("select").filter(hasValue);

My actual selectors are a little larger, checking multiple elements with or without values so now I'm having to split my 1 selector string into multiple selectors with .filter(..) method calls in between.

Question

Is there an equivalent to $("[value=]"), $("[value!=]"), $("[value='abc']") which uses the property instead of the attribute? And if not, is there a cleaner way than using the .filter(hasValue) and .filter(hasNoValue) methods?

Thanks

解决方案

Using .filter seems to be the only way, but it's not too bad and you can actually make it a little more accurate by using .val:

$(":input").filter(function () { return $(this).val() === ""; });

If this really is that reprehensible to you, you could create a custom selector.

$.expr[':'].emptyInput = function (elem) {
    return $(elem).is(":input") && $(elem).val() === "";
};

http://jsfiddle.net/ExplosionPIlls/zaZPp/

EDIT: You may also be able to get away with using this.value instead of $(elem).val().

这篇关于jQuery 1.9.1属性选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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