Javascript Jquery名称选择器
遇到这个问题:Javascript Jquery名称选择器,javascript,jquery,Javascript,Jquery,遇到这个问题: <input type="checkbox" name="fasdf" value="1" id="dsasd" ></input> <input type="checkbox" name="fasdf" value="2" id="hjiuhhjb" ></input> <script type="text/javascript" src="/asdf/js/jquery-1.11.0.min.js"></scr
<input type="checkbox" name="fasdf" value="1" id="dsasd" ></input>
<input type="checkbox" name="fasdf" value="2" id="hjiuhhjb" ></input>
<script type="text/javascript" src="/asdf/js/jquery-1.11.0.min.js"></script>
<script>
var numberChecked = jQuery("input[name='fasdf']").length;
alert(numberChecked);
jQuery("input[name='fasdf']").attr('checked', true);
</script>
var numberChecked=jQuery(“输入[name='fasdf']”);
警报(已检查编号);
jQuery(“输入[name='fasdf']”).attr('checked',true);
numberChecked始终返回1,并且选中第一个复选框。如果我更改第一个复选框的名称,那么第二个复选框将被选中。为什么?
提前谢谢
已编辑:函数已删除因为您为复选框指定了相同的名称。
某些jQuery函数只针对第一个匹配的元素(在您的情况下,您针对的是多个元素)。
更改第一个复选框名称时,唯一针对的复选框是
#hjiuhhjb
这里有一个简单的解决方案:
jQuery("input[name='fasdf']").each(function(){
$(this).attr('checked', true);
});
.each()
将迭代所有匹配的元素(更多:)在这里返回2作为长度是的,所以我不明白。也许我在某处有一些奇怪的JS代码。如果你能创建fiddle,那就太好了。你在哪里调用了documento函数?函数没有在这个代码块中调用。好了,伙计们,问题是你做错了什么。正在研究IE8。谢谢大家:)