jquery:选择具有给定名称和值的输入

前端之家收集整理的这篇文章主要介绍了jquery:选择具有给定名称和值的输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检查名为weekday的输入,并且value = 1.我尝试下面的行.它检查所有平日.
$('input[name = weekday],[value =1]').attr("checked","checked");

解决方法

不要使用逗号在同一个元素上同时应用这两个条件.
$('input[name=weekday][value=1]').attr("checked","checked");

作为附注,您应该使用prop()而不是attr()用于jQuery doc建议并由@tyleha指向的属性.

As of jQuery 1.6,the .attr() method returns undefined for attributes
that have not been set. To retrieve and change DOM properties such as
the checked,selected,or disabled state of form elements,use the
.prop() method.

您可以使用.prop( propertyName,value )设置选中的属性,如下所示.

$('input[name=weekday][value=1]').prop("checked",true);

猜你在找的jQuery相关文章