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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题