每次在php中选中复选框时调用ajax [英] Call ajax each time when a checkbox is checked in php

查看:73
本文介绍了每次在php中选中复选框时调用ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中有一些复选框。现在,当我按下提交btton时,即时调用ajax.but我希望ajax在任何复选框被选中时被调用。

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

我的代码如下调用ajax。

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>

目前我在按下按钮时调用ajax,但是我想每次调用ajax并获取结果时任何复选框都会被检查。
请告诉我在上面的代码中可以更改或添加的内容。

Currently i m 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.

推荐答案

使用change事件来改变复选框,并使用prop来检查他是否被选中。

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

这篇关于每次在php中选中复选框时调用ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆