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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里