如何将php数组值传递给jquery选择框。
我试过
$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,现在获取客户名称
答案 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);
});