没有从我的AJAX请求中获取任何Checkbox值

时间:2016-05-18 11:13:13

标签: jquery ajax

我有一个复选框,其中包含SQL生成的值。我想发送由AJAX检查的复选框的值,以便能够在另一个元素的同一页面上使用。但我在测试'警报'框中没有得到任何值。谁能指出我做错了什么?

<form id="numberOrderForm" action="index.php" method="POST">
    <div class="wrappers" id="multi-select1Wrapper">
        <h2>Area Code</h2>
        <select class="dropDownMenus" id="multi-select1" name="multi_select1[]" multiple="multiple">
            <?php
                //The query asking from our database
                $areaCodeSQL = "SELECT ac.Number AS `AreaCode`, ac.Name AS `AreaName`
                                FROM `AreaCodes` ac";                                                               //SQL query: From the table 'AreaCodes' select 'Number' and put into 'AreaCode', select Name and put into 'AreaName'

                $areaCodeResults = $conn->query($areaCodeSQL);                                                      // put results of SQL query into this variable

                if ($areaCodeResults->num_rows > 0) {                                                               // if num_rows(from $results) is greater than 0, then do this:
                    // output data of each row
                                foreach($areaCodeResults as $areaCodeResult)                                        //for each item in $areCodeResults do this:
                                    {
                                        $areaNameAndCode =  $areaCodeResult['AreaCode'] ." ". $areaCodeResult['AreaName'];  //get AreaCode and AreaName from query result and concat them
                                        $areaName = $areaCodeResult['AreaName'];                                    // get AreaName
                                        $areaCode = $areaCodeResult['AreaCode'];                                    //get AreaCode

                                        ?><option class="menuoption1" name="menuAreaCode" value="<?php echo $areaCode ?>"  ><?php echo $areaNameAndCode; ?></option><?php  //Create this option element populated with query result variables
                                    }
                } 

            ?>
        </select>
    </div>

我的AJAX请求:

<script>
    $(function(){
        $("#multi-select1").change(function(e){
                e.preventDefault();
                var items=$("input[name='multi_select1']:checked").each(function () {
                    return this.value;
                }).get().join(',');



                $.post("index.php?data="+items, function(response){
                  alert(items);
                });        
           });           
        });
</script>

2 个答案:

答案 0 :(得分:1)

  

请把你的脚本放在这个

<script>
$(function(){
    $("#multi-select1").change(function(e){
        items = $("#multi-select1").val()
            $.post("index.php?data="+items, function(response){
              alert(items);
            });        
       });           
    });

答案 1 :(得分:0)

您正在index.php文件的顶部看到类似的内容

if (isset($_GET["data"])) { 
    // do what you want to do with data, add it to that form etc...
    exit(); // add this in order to prevent script from processing rest of page.
}

这应该这样做。