【问题标题】:.remove() is not working when checkbox's unchecked未选中复选框时,.remove() 不起作用
【发布时间】:2017-05-17 21:54:38
【问题描述】:

我在尝试选中和取消选中复选框时遇到了一个小问题。当我检查它时,它显示以下图片:

它应该出现按钮,删除,重命名和共享,附加到上传和新文件夹。但是取消选中后我无法删除附加。

我的代码如下:(jQuery)

var ids = <?php echo json_encode($arr); ?>;
for(i = 0; i < ids.length; i++) {               
                var sel = $("input[id='" + ids[i] + "']");
                $(sel).on('click', function(e) {
                    e.stopPropagation();
                    $.each(sel, function( sel ) {
                        if($(sel).is(':checked')) {
                            $("#del").remove();
                            $("#rename").remove();
                            $("#sharing").remove();
                            $(sel).prop('checked', false );
                        } else {
                            $(sel).prop('checked', true );
                            $("#del").remove();
                            $("#rename").remove();
                            $("#sharing").remove();
                            $(".sub-menu-files ul").append("<li class=\"delbtn\"><button id=\"del\">Delete File</button</li> <li class=\"renamebtn\"><button id=\"rename\">Rename</button></li> <li class=\"sharingbtn\"><button id=\"sharing\">Sharing</button></li>");
                            $("#del").click(function() {
                                var sels = $("tbody tr input");

                                $(sels).each(function() {
                                    if($(this).is(':checked')) {
                                        alert($(this).attr('id') + " has been deleted");
                                        $.post("delete.php", {id: $(this).attr('id') }, function(data) {
                                            if(data != "") {
                                                alert("Your file/files have been sent to the Trash. To delete them from our server, go to \"Deleted Files\" and delete it.");
                                            }
                                        });
                                    }
                                });
                            });
                        }
                    });
                });


            }           

ids 将在数据库中选择上传文件的 ID。我尝试了一切,但没有任何好处。

我的 php + HTML 内容是:

<td><input type="checkbox" name="select" class="case" id="<?php echo $rows['id']; ?>" style="cursor: pointer;" /></td>

也在挑选id。

有没有办法让它工作?

【问题讨论】:

    标签: javascript php jquery html checkbox


    【解决方案1】:

    它不起作用,因为当脚本第一次运行时,这些元素还不存在。

    因此,你应该通过一个一直存在的元素来引用它们,body 是好的。

    $('body').find('#del').remove();
    

    这应该可以工作

    【讨论】:

    • 它不起作用...当我取消选中它时,它会停留在那里并且不会删除。
    【解决方案2】:

    试试这个。您甚至不需要嵌入式 PHP 部分。真的是两个处理程序。

    //handler for #del.  you only need to declare this once, as a global.
    $(document).on("click", "#del", function() {
      var sels = $("tbody tr input:checked");
    
      $(sels).each(function() {
    
        alert($(this).attr('id') + " has been deleted");
        $.post("delete.php", {
          id: $(this).attr('id')
        }, function(data) {
          if (data != "") {
            alert("Your file/files have been sent to the Trash." +
              " To delete them from our server, go to \"Deleted Files\" and delete it.");
          }
        });
    
      });
    });
    
    //handler for check boxes
    $(document).on("change", "tbody tr input", function(e) {
      //See if any are checked.
      if ($("tbody tr input:checked").length > 0) {
        {
          $(".sub-menu-files ul").append("<li class=\"delbtn\"><button id=\"del\">Delete File" +
            "</button</li> <li class=\"renamebtn\"><button id=\"rename\">" +
            "Rename</button></li> <li class=\"sharingbtn\">" + " <
            button id = \"sharing\">Sharing</button></li>");
        } else {
          $("#del, #rename, #sharing").remove();
        }
      };
    });

    【讨论】:

      【解决方案3】:

      我发现了错误。只是将 $(sel).on('click', function() { 函数更改为 $.each(sel, function(sel) { 函数内部,然后将 selon('click')this

      所有试图帮助我的人,我都想感谢它。

      【讨论】:

        猜你喜欢
        • 2016-11-25
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多