dt97868 2017-07-25 05:29
浏览 49
已采纳

取消选中“全部选中”复选框后,提交按钮不会隐藏

I am trying to make a page where submit button show/hides when a checkbox is checked. The same I want to do with the 'check-all' and currently when check-all is checked, the submit button appears but when the 'check-all' gets unchecked the submit button does not hides. Although the listed checkboxes perforn absolutely fine but only the unchecking of 'check-all' does not makes the submit button hide.

here is my code, please let me know where I am going wrong and if there is any solution for it.

<input type="checkbox" id="select_all" name="all_check[]" class="checkbox" value= "<?php echo $row['id']; ?>"/> </th>  //this is my 'check-all'

<input type="checkbox" name="check[]" <?php echo $disabled ;?> value= "<?php echo $row['id']; ?>"class="checkbox" id="select_all" >  //this is my listed checkboxes under the 'check-all'

and here is my jquery,

$(function(){
    $('[type=checkbox]').click(function ()
    {
        var checkedChbx = $('[type=checkbox]:checked');
        if (checkedChbx.length > 0)
        {
            $('#one').show();
        }
        else
        {
            $('#one').hide();
        }
    });
});


  $(document).ready(function() {

        var $submit = $("#one").hide(),
            $cbs = $('input[name="all_check[]"]').click(function() {
                $submit.toggle( $cbs.is(":checked") );
            });

    });
  • 写回答

4条回答 默认 最新

  • dongzhouzhang8696 2017-07-25 05:40
    关注

    You didn't store the selector in the $submit variable, try:

    var $submit = $("#one");
            $submit.hide();
               var $cbs = $('input[name="all_check[]"]').click(function() {
                    $submit.toggle( $(this).is(":checked") );//use this to get the current clicked element 
                });
    

    change the selector for the first click event to

     $('input[name="check[]"]').click(function () {....
    

    demo:

    $(function() {
      $('input[name="check[]"]').click(function() {
        var checkedChbx = $('[type=checkbox]:checked');
        if (checkedChbx.length > 0) {
          $('#one').show();
        } else {
          $('#one').hide();
        }
      });
    });
    
    
    $(document).ready(function() {
    
      var $submit = $("#one");
      $submit.hide();
      var $cbs = $('input[name="all_check[]"]').click(function() {
      $('input[name="check[]"]').prop('checked',$(this).is(":checked"));
        $submit.toggle($(this).is(":checked")); //use this to get the current clicked element 
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" id="select_all" name="all_check[]" class="checkbox" value="<?php echo $row['id']; ?>" /> </th> //this is my 'check-all'
    
    <input type="checkbox" name="check[]" class="checkbox" id="select_all">
    <input type="checkbox" name="check[]" class="checkbox" id="select_all">
    <input type="checkbox" name="check[]" class="checkbox" id="select_all">
    <input type="checkbox" name="check[]" class="checkbox" id="select_all">
    <input type="checkbox" name="check[]" class="checkbox" id="select_all"> //this is my listed checkboxes under the 'check-all'
    <button id="one">one</button>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题