当“全部检查”时,提交按钮不会隐藏取消选中复选框

时间:2017-07-25 05:29:42

标签: php jquery checkbox

我正在尝试在选中复选框时创建提交按钮显示/隐藏的页面。同样我想用' check-all'目前,当选中全部检查时,会出现提交按钮,但是当“全部检查”时取消选中提交按钮不会隐藏。虽然列出的复选框绝对可以,但只能取消选中“全部检查”复选框。不会使提交按钮隐藏。

这是我的代码,请告诉我哪里出错了,以及是否有任何解决方案。

<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'

这是我的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 个答案:

答案 0 :(得分:2)

以下是取消选中所有复选框时如何隐藏的示例....

&#13;
&#13;
$("input[type=checkbox]").click(function(){
  if($("input[type=checkbox]:checked").length > 0)
  {
    $("input[type=submit]").show();
  }
  else{ 
    $("input[type=submit]").hide(); 
  }
});

$(document).ready(function(){
 $("input[type=submit]").hide(); 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input type="checkbox" name="chk1[]" />check 1
<input type="checkbox" name="chk1[]" />check 2
<input type="checkbox" name="chk1[]" />check 3

<input type=submit />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您没有将选择器存储在$ submit变量中,请尝试:

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 
            });

将第一次点击事件的选择器更改为

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

演示:

&#13;
&#13;
$(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 
  });

});
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

$("input[name='check']").on('change',function(){
  if( $("input[name='check']:checked").length>0)
     $("input[type='submit']").fadeIn();
  else
     $("input[type='submit']").fadeOut();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="submit" value"submit" style="display:none;"/>

答案 3 :(得分:0)

尝试一下:

<script type="text/javascript">
$(".YourCheckBoxClass").change(function() {
    $('#YourSubmitButtonID').prop('disabled', $(".check_class").filter(':checked').length < 1);
});
$(".YourCheckBoxClass").change();
</script>