jQuery / Javascript - submit form when checkbox is

2020-06-21 10:39发布

问题:

I have a page where I display results form database and there are checkboxs which filter ther results. The problem is that I don't want to use submit button because I would like to let the user only click on the checkboxes to get the desired results. So is there anyway using javascript or jquery to submit a form when checkbox is checked with no submit button? if you see on shopping sites there are no submit buttons just the checkboxes..Like that. thanks

<form>
<input type="checkbox" name="size" > Size
</form>

here is the php

<?php if (isset($_POST["size"])){
  //do something
?>

回答1:

<form id="form" method="post" action="">
    <input type="checkbox" name="checkbox" class="checkbox"/>
    </form> 
    <script type="text/javascript">  
        $(function(){
         $('.checkbox').on('change',function(){
            $('#form').submit();
            });
        });
    </script>

OR

<form id="form" method="post" action="">
<input type="checkbox" onchange="$('#form').submit();"  name="checkbox" class="checkbox"/>
</form>


回答2:

$(input:name=[checkboxname]).change(function(){

 $('#form').submit();

});

alternately

 $('#checkboxId').click(function(){

    $('#formId').submit();

  });

of course additional parameters can be passed withing the submit() function for ajax, etc.