jquery - 选中/取消选中 jquery mobile 中的所有复选框失败

标签 jquery jquery-mobile

我有一个带有两个按钮的 jquerymobile 页面。一两次选中所有复选框,一则取消选中所有复选框。 第一次点击检查全部时,它可以工作,但取消选中后,它就不再工作了。

这是我的代码:

<div data-role="page" id="selecttest">

    <div data-role="header">
        <a href="#sc" data-icon="home">SSC</a>
        <h1>...</h1>
    </div><!-- /header -->

    <div data-role="content">
        <fieldset class="ui-grid-a">
            <div class="ui-block-a"><button onclick="$('#selecttest input[type=checkbox]').attr('checked','checked').checkboxradio('refresh');" data-theme="b">check all</button></div>
            <div class="ui-block-b"><button onclick="$('#selecttest input[type=checkbox]').removeAttr('checked').checkboxradio('refresh');" data-theme="b">uncheck all</button></div>
        </fieldset>
        <fieldset data-role="controlgroup">
            <input type="checkbox" name="checkbox-v-2a" id="checkbox-v-2a">
            <label for="checkbox-v-2a">One</label>
            <input type="checkbox" name="checkbox-v-2b" id="checkbox-v-2b">
            <label for="checkbox-v-2b">Two</label>
            <input type="checkbox" name="checkbox-v-2c" id="checkbox-v-2c">
            <label for="checkbox-v-2c">Three</label>
        </fieldset>
    </div>
</div><!-- /page -->

我正在使用 jQuery 1.9.1 和 jQuerymobile 1.3

我已经看过 How to select or unselect all checkboxes in JQuery Mobile?但这对我没有帮助。

最佳答案

jQuery 1.9 恢复了 1.6 中对 .attr 所做的更改并在 1.6.1 中删除。这意味着 .attr.prop 现在又恢复严格了。如果需要操作属性,请使用 .prop,否则,请使用 .attr。您实际上想要使用该属性的情况非常罕见。

对于复选框的选中状态,您应该使用 .prop("checked",true).prop("checked",false)

关于jquery - 选中/取消选中 jquery mobile 中的所有复选框失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15031831/

相关文章:

javascript - 删除html表格中的空行

jquery - 如何在 JQuery 中使用 @ViewBag 值

jquery - 向右移动聊天气泡

jQuery mobile "page"单击链接时未加载

javascript - jquery 移动搜索按钮

javascript - jquery-mobile ListView -> 刷新事件

jquery-mobile - JQueryMobile - 按钮

jQuery.text() set 修改多于 get

jquery - 验证文件上传 - Jquery 和 "Accept"属性

javascript - DIV 中支持 jQueryMobile 的站点