如何在函数后使复选框长度为nil

How to make checkbox length nil after fucntion

本文关键字:nil 复选框 函数      更新时间:2023-09-26

我有几个复选框和一个按钮,用于将选中的内容发送到垃圾箱。我的功能是选择并将它们发送到垃圾工厂。但是我的按钮发送到垃圾,应该只出现当复选框被选中和消失时,他们移动到垃圾。但是我的按钮不会消失,因为我的复选框值在删除时没有重置为零。任何想法如何重置我的复选框长度后移动到垃圾文件。我的代码如下,

$('input[name=myfile_select_id]').change(function() {
    console.log("Touched");
    console.log($('input[name=myfile_select_id]:checked').length);
    if ($('input[name=myfile_select_id]:checked').length > 1) {
        // one or more checkboxes are checked
        $("#trash-select").show();
    } else {
        // no checkboxes are checked
        $("#trash-select").hide();
    }
});
$("#trash-select").click(function() {
    console.log("CLI");
    var selectedFiles = $('input[name=myfile_select_id]:checked');
    var checkValues = selectedFiles.map(function() {
        return $(this).val();
    }).get();
    if (confirm("Are you sure, you want to move these files to trashcan?")) {
        $.ajax({
            url: '/myfiles/multiple_select',
            type: 'post',
            data: {
                myfile_ids: checkValues
            },
            success: function(data) {}
        }).done(function() {
            selectedFiles.each(function(index) {
                console.log("I'm done");
                $(this).closest('tr').remove();
            });
        });
    } else {
        console.log("not passed");
    }
});

我的xyz.html.erb文件

<div class="File--Select">
                <%= check_box_tag "myfile_select_id", myfile.id , false, :id => "myfile-select-#{myfile_path(myfile).to_s.split('/')[2]}"  , :class => "file-select" %>
</div>  
<div class="pull-right">
  <%= button_tag :id =>"trash-select", :class => "btn btn-sm btn-danger", :style => "display:none" do %>
  <span class="" aria-hidden="true"></span> Move to Trash
  <% end %> &nbsp;
</div>

任何帮助都是感激的。TIA。

使用这个。可能对你有帮助

function reset(selectedFiles){ 
   if(selectedFiles.length < 1) { $("#trash-select").hide();} }//can you call this function in ajax done function once removed the checkedbox row.

您应该在删除复选框后以编程方式触发' change '事件。

//You don't need each
selectedFiles.closest('tr').remove();
//Trgger cgnage event
$('input[name=myfile_select_id]').change();

需要在。done上隐藏按钮,因为更改事件不会在tr移除时触发。

 .done(function() {
        selectedFiles.each(function(index) {
            console.log("I'm done");
            $(this).closest('tr').remove();
         // hide button on done
            $("#trash-select").hide();
 });