通过 jQuery 获取输出 json_encode() 的值自定义。?
我使用 json_encode()
在数据库中插入数据,希望仅获取数据库中行 units
上的值 name_units
。如何?
这是代码 php 中的输出数据库(通过 json_encode()
):
my_table=>units=>name_units
[{"name":"jack","units":[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]}, {“name_units”:“莫赫勒斯”,“price_uni ts":"4,851,269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["迷你吧","mobleman ","电视"]}]}]
var dataObj = $(this).closest('form').serialize();
$.ajax({
type: "POST",
dataType: 'json',
url: 'url',
data: dataObj,
cache: false,
success: function(data) {
/////////////////////*HERE/////////////////////
$.each(data, function(a, b) {
//alert(b.units[name_units]);
$('<p id="' + b.units[name_units] + '">' + b.units[name_units] + '</p>').appendTo('.class');
});
/////////////////////HERE*/////////////////////
};
})
I insert data with json_encode()
in database, want get just values name_units
on row units
in database. how is it?
This is output database in code php(by json_encode()
):
my_table=>units=>name_units
[{"name":"jack","units":[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]},{"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]}]
var dataObj = $(this).closest('form').serialize();
$.ajax({
type: "POST",
dataType: 'json',
url: 'url',
data: dataObj,
cache: false,
success: function(data) {
/////////////////////*HERE/////////////////////
$.each(data, function(a, b) {
//alert(b.units[name_units]);
$('<p id="' + b.units[name_units] + '">' + b.units[name_units] + '</p>').appendTo('.class');
});
/////////////////////HERE*/////////////////////
};
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
此外,对于这种情况使用 jQuery 模板 是个好主意
Try this:
Also, its a good idea to use jQuery templates for this scenario
units 是一个数组,因此您需要引用数组中的一个项目或循环遍历该数组中的所有项目。
b.units[0].name_units
编辑:就像我说的,你必须循环遍历它们。
如果您愿意,也可以使用 for 循环。
units is an array, therefore you need to reference an item in the array or loop through all items in that array.
b.units[0].name_units
Edit: like i said, you would have to loop through them.
you could also use a for loop if you would prefer.