简体   繁体   中英

Call ajax each time when a checkbox is checked in php

I have a page in which there are some checkboxes.Right now when ever i press submit btton, im calling ajax.but i want the ajax to be called when any of the checkboxes is checked.

My code is as below for calling ajax.

<html>
<head>
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    function get_check_value() {
        var c_value = [];

        $('input[name="brand"]:checked').each(function () {
            c_value.push(this.value);
        });
        return c_value.join(',');

    }
    function get_store_value(){
    var d_value=[];
        $('input[name="store"]:checked').each(function () {
            d_value.push(this.value);
        });
        return d_value.join(',');
        }

    $(document).ready(function(){
   $('#btnSubmit').on('change', function (e)
   {e.preventDefault();
    alert("hi");
        //var os = $('#originState').val();
       //var c = $('#commodity').val();
        //var ds = $('#destState').val();
        var ser = get_check_value();
        var store=get_store_value();
        //var queryString = "os=" + os;
        var data = "?ser=" + ser;
        var queryString = "&ser=" + ser;
        alert(ser);
       $.ajax({
       //alert("ajax");
        type: "POST",
        url: "sortingajax.php",
        data: {ser:ser,store:store},
        dataType :  'html',
        success: function (b) {
           // alert(a+' ok. '+b)
            $('#results').html(b);
            console.log(b);
        }
    });

  });
 });

</script>


brand
    <input type="checkbox" name="brand" value="Sunbaby" id="check" />Sunbaby
    <br/>
    <input type="checkbox" name="brand" value="Advance Baby" id="check"/>Advance Baby
    <br/>
    store
    <br/>
    <input type="checkbox" name="store" value="JC Penny" id="check"/>JC Penny
    <br/>
    <input type="checkbox" name="store" value="Supermart" />Suoermart
    <br/>
    <input type="checkbox" name="store" value="Target" />Target
    <br/>


<button id="btnSubmit">sort</button>
<div id="results">
</div>
</body>
</html>

Currently im calling ajax on pressing button,but i want to call ajax each time and fetch the results when ever any of the checkbox is checked.. Please tell me what can i change or add here in above code.

只需更改您的事件,然后处理您的复选框,即可:

$( "[type=checkbox]" ).change(function () { ...

Use the change event for a changed checkbox and use the prop for checking if he is checked.

$("[type=checkbox]").change(function () {
    if(this).prop('checked')
    {
        // textbox is checked
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM