[英]In jquery get value from array by key name
I have One Program where i get information contains in array and i want to aceess it with using key. 我有一个程序,我在其中获取信息,该信息包含在数组中,我想使用键来增加它。
Is there any Way for this ? 有什么办法吗?
Here are code example : 这是代码示例:
jQuery("#sl_name").change(function(){
var id = this.value;
jQuery.ajax({
url : 'insert.php',
type:'post',
data:{'action': 'getById','id':id},
success : function(data){
alert(data);
}
});
});
Here variable Data contain array of user i need to access by key value like in php we can get. 在这里,变量数据包含我需要通过键值访问的用户数组,例如我们可以获取的php。
If you are return print_r than use json_encode method. 如果您返回print_r,则使用json_encode方法。
Thanks In Adavance. 感谢先进。
don't use alert to debug objects, you will always get [Object object]
, use console.log
instead: 不要使用警报来调试对象,您将始终获得
[Object object]
,而使用console.log
:
jQuery("#sl_name").change(function () {
var id = this.value;
jQuery.ajax({
url: 'insert.php',
type: 'post',
data: {
'action': 'getById',
'id': id
},
success: function (data) {
console.log(data);
//if data is not yet a json object
data = JSON.parse(data);
//To access by key/prop
console.log(data.username);
//if data is an array:
for (var i = 0; i < data.length; i++) console.log(data[i]);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.