javascript - 如何从jquery标记选中的所有复选框

标签 javascript php

我想使用 JavaScript 通过单击来选中和取消选中复选框列表:

<table width="100%" border="0" cellspacing="1" cellpadding="1" align="center">
<tr>
    <td width="10%"><input type="checkbox" name="All" value="All" checked="checked" id="SelectAll1" class="MemberSelectcheckBox1" /></td>
    <td width="90%"><strong>Field Name</strong>:</td>
</tr>
<tr>
    <td><input type="checkbox" name="FirstName" value="1" id="FirstName" class="MemberSelectcheckBox1" checked="checked"></td>
    <td>First Name</td>
</tr>
<tr>
    <td><input type="checkbox" name="Surname" value="1" id="Surname" class="MemberSelectcheckBox1" checked="checked"></td>
    <td>Surname</td>
</tr>
<tr>
    <td><input type="checkbox" name="Email" value="1" id="Email" class="MemberSelectcheckBox1" checked="checked"></td>
    <td>Email</td>
</tr>
<tr>
    <td><input type="checkbox" name="Phone" value="1" id="Phone" class="MemberSelectcheckBox1" checked="checked"></td>
    <td>Phone</td>
</tr>
<tr>
</tr>
</table>

和下面提到的 JavaScript 仅适用于未选中的情况:

<script type="text/javascript">

$("#SelectAll1").click(function(){
    if($(this).is(':checked')){
        //$(".MemberSelectcheckBox1").attr('checked','checked');
    }else{
        $(".MemberSelectcheckBox1").removeAttr('checked');
    }
});

</script>

最佳答案

这是解决方案;使用.prop()代替attr()removeAttr(),并且最好使用on() > 而不是 click():

$(document).on('click', '.#SelectAll1', function () {
    if($(this).is(':checked')) {
        $(".MemberSelectcheckBox1").prop("checked",true);
    } else {
        $(".MemberSelectcheckBox1").prop("checked",false); 
    }
});

关于javascript - 如何从jquery标记选中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624225/

相关文章:

javascript - 确定 html 标签的内容何时更改

javascript - 当我将对象传递给 JavaScript 函数时,该对象是否通过引用传递?

php - 为什么 JSON 只为我的查询返回 1 个图像输出?

php - 具有多个 ORDER BY Field 语句的 SQL 查询 - 或者可能是一种新方法?

javascript - 将 JavaScript 值以明文形式保存在数据库中

javascript - 意外的函数表达式。 (首选箭头回调)

javascript - 使用 html2canvas 设置 Png 的质量

javascript - 如何使用 Google Analytics 设置页面标题?

URL 查询的 PHP parse_str 返回键名中包含 'amp' 的数组键

php - 如何在foreach循环中获取唯一值