多选ajax请求显示错误以及如何从多选框中获取所选值


multiple selector ajax request showing error and how to get the selected value from multiple select box?

下面是我的代码。我的工作是,根据城市,地点的变化。因此,一旦我选择了城市,就会发送一个Ajax请求,并重置位置。这个位置是多选框,所以我可以选择多个位置。基于此,我想显示分支。这里我的问题是,当我更换城市时,会出现多个下拉列表。当我打印时alert($('#cboLocation option:selected').val())它唯一的警报第一个值

PHP代码

<?php
     $arrCity = array('Chennai' => 'Chennai',
                         'Delhi'  => 'Delhi',
                         'Noida'  => 'Noida');
       $act     =   formatstring($_POST['act']);        
      switch($act)  
            {
                case "getCommonLocation":
                    $dbConn       = setDbConn();
                    $strCityName  = $_POST['CityName'];             
                    $strSQL = "SELECT distinct locality 
                                 FROM mp_new_project 
                                WHERE city = '".$strCityName."' ORDER BY locality ASC";         

                    $stmt = $dbConn->prepare($strSQL);
                    $stmt->execute();
                   // $stmt->bind_result($LocationId, $Location);
                   $stmt->bind_result($Location);
                    while($stmt->fetch())
                     //$arrLocations[] = array($LocationId, $Location);;   
                       $arrLocations[] = array($Location);              
                     for($i=0;$i<count($arrLocations);$i++)
                      $strOptionsList .= "<option value='".$arrLocations[$i][0]."'>".$arrLocations[$i][0]."</option>";  
                    $stmt->close();
                    $dbConn->close();
                    print "<option value='all'>All</option>".$strOptionsList;
                    exit();
                break;
            }
    ?>

Javascript

 <script type="text/javascript" src="scripts/jquery-1.6.1.min.js"></script>
    <script type="text/javascript" src="scripts/jquery-ui-1.8.13.custom.min.js"></script>
    <script type="text/javascript" src="scripts/ui.dropdownchecklist-1.4-min.js"></script>
    <script type="text/javascript">
     function chnageToUP()
        {
         //  var selLoc =  $('#cboLocation option:selected').text();
          alert($('#cboLocation option:selected').val());
          return false;
        }
        function getCommonLocationForCities()
        {
        url         = 'testing1.php';
        strCityName =  $('#cboCity').val();
        //chnageToUP();
        $.post(
            url,
            {
                "act"       : "getCommonLocation",
                "CityName"  : strCityName
            },
            function(responseText){ 
                $('#cboLocation').val("");
                $('#cboLocation').html(responseText);
                $("#cboLocation").dropdownchecklist({firstItemChecksAll: true, 
                                                     maxDropHeight: 100,
                                                     onComplete: function(selector)
                                                     {
                                                        chnageToUP();
                                                      }});  
                },
            "html"
        );      
       }
    </script>

HTML代码

<table style="margin-left:50px;" cellpadding="3" cellspacing="1" border="0" >
<tr>
     <td><b>City</b></td>
     <td><select name="cboCity" id="cboCity" onchange="getCommonLocationForCities()">
               <option  value="">City</option>
                <?php 
                   foreach($arrCity as $item)
                   {
                      print  "<option value='".$item."'>".$item."</option>";
                   }
                  ?>
       </select></td>
</tr>
<tr>
      <td><b>Location</b></td>
      <td> 
        <select name="cboLocation" id="cboLocation" class="select_142" multiple="multiple">   
         <option value='location'>Location</option>         
        </select>
      </td>
</tr>  
</table>

以下是链接http://vignesh.gvignesh.org/dropdown/

使用map获取数组中的检查值。。。。

试试这个

 function chnageToUP()
 {
     var tempArray="";
     tempArray=$('#cboLocation :checked').map( function() { return this.value; });
     console.log(tempArray);
     return false;
 }

alert()不是一个很好的调试方法,请改用console.log()。要求您拥有Firebug或Google Chrome才能查看内容。