简体   繁体   English

单击按钮时未选中复选框

[英]Checkbox is not selected when click on the button

I'm trying to get a checkbox before the list items in the listview. 我正在尝试在listview中的列表项之前获得一个复选框。 I have a page with listview when I click on the "edit" button. 单击“编辑”按钮时,我的页面带有列表视图。 The listview adding the checkboxes before the list items but when I'm clicking on the checkbox's the page is automatically changes to the previous view (without checkboxes in listview).Checkboxes are not checked.where I'm doing wrong. 列表视图在列表项之前添加了复选框,但是当我单击复选框的页面时,页面会自动更改为上一个视图(列表视图中没有复选框)。未选中复选框。我做错了。

$('#fav').on('click', function() {
    $("#favoritesList").listview('refresh');
    fromStorage();

    $(document).on('click', '#empty', function() {
        $("#favoritesList").empty();
        localStorage.clear();
        $("#favoritesList").listview('refresh');
    });


    $(document).on('click', '#edit', function() {
        fromGetStorage();
    });
});

function fromGetStorage() {
    $("#favoritesList").empty();
    $(".ui-listview-filter").remove();
    $("#favoritesList").append('<input type="checkbox" name="all" id="select" />Select all</br>');
    for (var i = 0; i < localStorage.length; i++) {
        var url = localStorage.key(i);
        var demo = localStorage.getItem(url);
        $("#favoritesList").append('<li><div><input type="checkbox" name="check" id="check">&nbsp;<a href="' + url + '" style="text-decoration:none;color:black">' + demo + '</a></div></li>').attr('url', url);
        $("#favoritesList").listview('refresh');
        $('#select').click(function() {
            if ($("#select").is(':checked')) {
                $("#check").prop("checked", true);
            }
            else {
                $("#check").prop("checked", false);
            }

            $("#favoritesList").listview('refresh');
}

As Thirumalai said, providing jsfiddle would help a lot. 正如Thirumalai所说,提供jsfiddle将有很大帮助。

I think your problem can be event bubbling in combination with how the jquery mobile works. 我认为您的问题可能是事件冒泡以及jquery mobile的工作方式。 jQuery mobile probably processes the listview and when it finds <a> inside, it automatically considers this as a "link part" of the item. jQuery mobile可能会处理列表视图,当它在内部找到<a> ,它会自动将其视为项目的“链接部分”。 When you click on the checkbox, apart from the checkbox being selected, click handler on the higher level is called as well. 当您单击该复选框时,除了选中该复选框外,还会调用更高级别的单击处理程序。

I would try to assing click handler to the checkbox and stop the bubbling of the event ( http://api.jquery.com/event.stopPropagation/ ) so it does not bubble to the higher level and does not open the link. 我会尝试将点击处理程序添加到复选框,并停止事件冒泡( http://api.jquery.com/event.stopPropagation/ ),以便它不会冒泡到更高的层次并且不会打开链接。

$("#check").click(function(event){
    event.stopPropagation();
});

If you don't need to stick with your code, I would suggest constructing the html similar way as in this post: 如果您不需要坚持自己的代码,建议您使用与本文类似的方式构造html:

Checkbox in ListView with Jquery mobile 使用Jquery mobile的ListView中的复选框

There are jsfiddles as well. 也有jsfiddles。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM