微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

原文地址:https://www.jb51.cc/jquery/179432.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐