Jquery没有从下拉列表中获取所选值

时间:2013-11-17 12:21:28

标签: javascript php jquery ajax

我正在尝试获取所选下拉值,所选值存储在数据库中,但不接受下面的选定值是我的代码可以指导如何操作。谢谢

更新

clientnetworkpricelist / update.php

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxx';
$dbPassword = 'xxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

$client_id=$_POST['clientid'];
    $feild=$_POST['field'];
            $data= $_POST['value'];
            $rownum=$_POST['rowid'];  
            $sql="UPDATE $client_id SET ".$feild." = '".$data."' WHERE net_id = ".$rownum."";

             print $sql;



            mysql_query($sql)

;  

HTML

<?php
$client_id=$_GET['clientid'];

if($client_id!=""){

$sql=mysql_query("select * from supplierprice a JOIN $client_id b WHERE b.`net_id` = a.`supp_price_id`" );
$query = "SELECT route FROM routestable WHERE `clientid` = '$client_id' ";
$result = mysql_query($query);
//print "query". $query;

while($rows=mysql_fetch_array($sql))
{

if($alt == 1)
        {
           echo '<tr class="alt">';
           $alt = 0;
        }
        else
        {
           echo '<tr>';
           $alt = 1;
        }

echo '   <td  class=" '.$rows["net_id"].'">'.$rows["clientid"].'</td>
         <td id="CPH_GridView1_clientid" class=" '.$rows["net_id"].'">'.$rows["region"].'</td>  
        <td id="CPH_GridView1_country" class=" '.$rows["net_id"].'">'.$rows["country"].'</td>
        <td id="CPH_GridView1_networkname" class=" '.$rows["net_id"].'">'.$rows["networkname"].'</td>
        <td id="CPH_GridView1_mcc"  class=" '.$rows["net_id"].'">'.$rows["mcc"].'</td>   
        <td id="CPH_GridView1_mnc"  class="'.$rows["net_id"].'">'.$rows["mnc"].'</td>
        <td id="CPH_GridView1_mnp" class="edit mnp '.$rows["net_id"].'">'.$rows["mnp"].'</td>';
  /*                        
         $ColumnNames = mysql_query("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = 'supplierprice' AND column_name NOT
IN ('supp_price_id','net_id','region', 'country', 'networkname', 'mcc', 'mnc', 'mnp'
)") or die("mysql error"); 

$columnArray=array();*/


foreach($columnArray as $value) {


//$columnArray[]=$rows1[0];

echo '<td  id="CPH_GridView1_xxx" width="0px;" class="edit '.$value.' '.$rows["net_id"].'">'.$rows[$value].'</td>';   
}   



 echo ' <td id="CPH_GridView1_clientprice" class="edit clientprice '.$rows["net_id"].'">'.$rows["clientprice"].'</td>
        <td> <select name="mySelect" id="mySelect" class="edit route '.$rows["net_id"].'" >';         



           // $query = "SELECT route FROM routestable WHERE `clientid` = '$client_id' `"; 
           // print "Query is".$query; 
            $result = mysql_query($query);
            while ($rows = mysql_fetch_assoc($result))  
            {  
                echo '<option value="' . $rows['route'] . '"> ' . $rows['route'] .     '</option>';  
               // echo '<option value="Hi"> Hi</option>';
            }   


         echo '</select>
                            </td>

                <td></td>'


        ;

echo '</tr>';

}
}
?>

AJAX

$(document).ready(function(){ 
    $('#mySelect').on('change keyup', function(){
        arr = $(this).attr('class').split( " " );
        var clientid=document.getElementById("client").value;

        $.ajax({    type: "POST",
            url:"clientnetworkpricelist/update.php",
           data: "value="+$('#mySelect').find(":selected").val()+"&rowid="+arr[2]+"&field="+arr[1]+"&clientid="+clientid,
            success: function(data){
            $('.ajax').html($('.ajax input').val());
            $('.ajax').removeClass('ajax');
        }});
    });


    $('#editbox').live('blur',function(){
        $('.ajax').html($('.ajax input').val());
        $('.ajax').removeClass('ajax');
    });
});

3 个答案:

答案 0 :(得分:2)

$('.ajax input').val()是一个问题,请确保您的HTML中有一个输入元素。

如果想在帖子中使用mySelect值,

data: "value="+$(this).val()+"&rowid="+arr[2]+"&field="+arr[1]+"&clientid="+clientid,

还在这里,

$('.ajax').html($('.ajax input').val());

在这种情况下,您需要使用edit类选择器。 ID是唯一属性,这就是原因,它适用于第一个元素。

             $('.edit').on('keyup change', function(){              

                  arr = $(this).attr('class').split( " " );

                  var clientid=document.getElementById("client").value;

                    $.ajax({    type: "POST",
                        url:"clientnetworkpricelist/update.php",
                        data: "value="+$(this).val()+"&rowid="+arr[2]+"&field="+arr[1]+"&clientid="+clientid,
                        success: function(data){
                        $('.ajax').html($('.ajax input').val());
                        $('.ajax').removeClass('ajax');
                    }});
                });

答案 1 :(得分:0)

您应该通过以下方式获取所选选项的值:

var value2send = $('#mySelect').find(":selected").val();

然后您可以将其添加到您的数据中。

<强>更新

感谢@charlietfl,这是另一种选择:

var value2send = $('#mySelect').val();

答案 2 :(得分:0)

要在下拉列表中获取所选项目,则无需使用CHange Action 例如

var SlcItem = $('#Your Select').find('option:selected').attr('value');