PHP数组到jquery选择框

时间:2016-07-19 09:34:16

标签: php jquery

如何将php数组值传递给jquery选择框。

我的表 enter image description here

我试过

$sql_customer = mysql_query("select * from tbl order by customer_name");
while($row_customer = mysql_fetch_array($sql_customer)){
    $customer_arr[$row_customer['id']] = $row_customer['customer_name'];
}

并以json编码值传递给jquery

var customerarray =;

var seloption = '';
$.each(customerarray, function (i, elem) {
    seloption += '<option value="'+customerarray[i]+'">'+customerarray[i]+'</option>'; 
});

如何在选项值中获取表ID,现在获取客户名称

3 个答案:

答案 0 :(得分:1)

只需将i设置为选项值,因为它包含一个cutomer id的数组的键。尝试:

var seloption = '';
$.each(customerarray, function (i, elem) {
    seloption += '<option value="'+i+'">'+customerarray[i]+'</option>'; 
});

答案 1 :(得分:0)

i是客户ID!

var seloption = '';
$.each(customerarray, function (i, elem) {
    seloption += '<option value="'
        + i + '">'
        + customerarray[i] + '</option>'; 
});

答案 2 :(得分:0)

如果没有连接和快速解决方法,这样做呢?

$(customerarray).each(function(i, e) {
  var o = new Option(e.customer_name, e.customer_id);
  $('#select-tag').append(o);
});