我有一个由链接调用的函数,它应该检查存在于特定 div 中的各种复选框(传递给函数。)在除 IE (7.) 之外的所有浏览器中工作。据我所知 .attr(' checked', 'checked' 是使用 jquery 1.5.1 执行此操作的正确方法
function selectall(industry){
$("#"+industry+"Apps :checkbox").attr('checked', 'checked');
$("#"+industry+"Apps :checkbox").change(); /* used for jqtransform plugin */
}
是否有我遗漏的东西或更好的方法可以在所有浏览器中运行?我真的不关心取消选择,只关心选择。
HTML 看起来像
<div id = 'HealthcareApps'>
<div style="clear: both;">
<a href = "javascript:selectall('Healthcare');">Select all</a>
</div>
<ul>
<li><label for="id_Healthcare_0"><input type="checkbox" name="Healthcare" value="1" id="id_Healthcare_0" /> Simple Messaging</label></li>
<li><label for="id_Healthcare_1"><input type="checkbox" name="Healthcare" value="2" id="id_Healthcare_1" /> Mobile Alerts and Alarms</label></li>
<li><label for="id_Healthcare_2"><input type="checkbox" name="Healthcare" value="3" id="id_Healthcare_2" /> Patient Identification / Drug Conflicts</label></li>
</ul>
(是的,我知道内联样式是个糟糕的主意。如果我能让这个链接在 IE 中工作,我也会修复它。)
最佳答案
您可以尝试这样的操作:js fiddle demo
<div id = 'HealthcareApps'>
<div style="clear: both;">
<a href="#" id="selectall" class="Healthcare" >Select all</a>
</div>
<ul>
<li><label for="id_Healthcare_0"><input type="checkbox" name="Healthcare" value="1" id="id_Healthcare_0" /> Simple Messaging</label></li>
<li><label for="id_Healthcare_1"><input type="checkbox" name="Healthcare" value="2" id="id_Healthcare_1" /> Mobile Alerts and Alarms</label></li>
<li><label for="id_Healthcare_2"><input type="checkbox" name="Healthcare" value="3" id="id_Healthcare_2" /> Patient Identification / Drug Conflicts</label></li>
</ul>
</div>
j查询:
$('#selectall').click(function(e) {
e.preventDefault();
var industry = $(this).attr("class");
$("#" + industry + "Apps :checkbox").attr('checked', 'checked');
});
并且,打开和关闭:
$('#selectall').click(function(e) {
e.preventDefault();
var industry = $(this).attr("class");
var checkbox = $("#" + industry + "Apps :checkbox");
checkbox.attr('checked', !checkbox.attr('checked'));
});
关于javascript - jquery 选择器/checked 属性在 IE7 中不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7151143/