$.each 使用时出现问题?
为值 salam & 的数据库行 units
赋值salavat :[此值由 json_encode()
插入]
salam:
[{"name_units":"salam","price_units":"74,554","checkbox_units":["迷你吧","mobleman"]},{"name_units":"mokhles","price_units":" 4,851, 269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["迷你吧","mobleman","tv “]}]
萨拉瓦特:
[{"name_units":"chaker","price_units":"5,452","checkbox_units":null},{"name_units":"khobe","price_units":"5,452,545","checkbox_units":[ "迷你吧","移动人"]}]
在示例中,执行以下操作:
示例: DEMO1-在这里运行我的代码 & ; DEMO2-这里只是用于显示所有代码
第一。请在输入=>中插入值:sala这有“拖”结果,请点击每个结果:salam 或 salavat ->您会看到单击=>后得到五个输出萨拉姆 & 莫赫勒斯 & 法达特 | chaker & khobe(如果必须有三个值,因为我们点击了单词 salam => salam & mokhles & 法达)
第二。请插入值:salam =>这有一个结果,请点击结果 (salam) ->您会看到单击 => 后得到“三”输出萨拉姆 & 莫赫勒斯 & fadat
我希望在任何情况下,结果搜索“五”或“三”值或“等”,获取与点击的单词相关的值name_units
。
$('.auto_complete').keyup(function () {
var dataObj = $(this).closest('form').serialize();
$.ajax({
type: "POST",
dataType: 'json',
//url: 'http://binboy.gigfa.com/admin/tour_foreign/auto_complete',
url: 'auto_complete',
data: dataObj,
cache: false,
success: function (data) {
var id_name = $('.list_autobox_hotel').attr('id');
$('.list_autobox_hotel').show().html('');
if (data == 0) {
$('.list_autobox_hotel').show().html('<p><b>there is no</b></p>');
} else {
$.each(data, function (index, value) {
$('<p id="' + value.name + '">' + value.name + '</p>').appendTo('.list_autobox_hotel');
});
//////////////////////*HERE//////////////////////
$('.list_autobox_hotel p').click(function (e) {
e.preventDefault();
var ac = $(this).attr('id');
$.each(data, function (index, value) {
$.each(value.units, function (bu, key) {
alert(key.name_units);
});
});
$(this).remove();
return false;
});
//////////////////////HERE*//////////////////////
$('body').click(function () {
$(".list_autobox_hotel p").hide().remove();
$('.auto_complete').val('');
$('.list_autobox_hotel').show().html('');
$('.list_autobox_hotel').css('display', 'none');
});
}
},
"error": function (x, y, z) {
// callback to run if an error occurs
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
});
更新:
对于bin,我这样做了:(但这不起作用,仍然存在同样的问题)
$('.list_autobox_hotel p').bind("click", function (e) {
e.preventDefault();
var ac = $(this).attr('id');
$.each(data, function (index, value) {
$.each(value.units, function (bu, key) {
alert(key.name_units);
});
});
$(this).remove();
return false;
});
我不明白“根据您点击的项目过滤数据“
不知道怎么回事!?
更新2
这是真的吗? (但这不起作用,并且有警报:未定义
)
$('.list_autobox_hotel p').bind("click", function (e) {
e.preventDefault();
var ac = $(this).attr('id');
var ok = $.grep(data, function (e) {
return e.name == ac;
}).units;
alert(ok);
$(this).remove();
return false;
});
Values the database row units
for values salam & salavat : [this values inserted by json_encode()
]
salam:
[{"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"]}]
salavat:
[{"name_units":"chaker","price_units":"5,452","checkbox_units":null},{"name_units":"khobe","price_units":"5,452,545","checkbox_units":["minibar","mobleman"]}]
In the example, do the following works:
EXAMPLE: DEMO1- in here work my code & DEMO2-in here just is for show all codes
First. please inserted value: sala in input => this have "tow" result, please clicked on each a of results: salam or salavat -> you see that get five output after click => salam & mokhles & fadat | chaker & khobe(In case must have three values, since we clicked on the word salam => salam & mokhles & fadat)
second. please inserted value: salam => this have one result, please clicked on result (salam) -> you see that get "three" output after click => salam & mokhles & fadat
I want in any case that is result search "five" or "three" value or "Etc.", get values name_units
related with word that is click.
$('.auto_complete').keyup(function () {
var dataObj = $(this).closest('form').serialize();
$.ajax({
type: "POST",
dataType: 'json',
//url: 'http://binboy.gigfa.com/admin/tour_foreign/auto_complete',
url: 'auto_complete',
data: dataObj,
cache: false,
success: function (data) {
var id_name = $('.list_autobox_hotel').attr('id');
$('.list_autobox_hotel').show().html('');
if (data == 0) {
$('.list_autobox_hotel').show().html('<p><b>there is no</b></p>');
} else {
$.each(data, function (index, value) {
$('<p id="' + value.name + '">' + value.name + '</p>').appendTo('.list_autobox_hotel');
});
//////////////////////*HERE//////////////////////
$('.list_autobox_hotel p').click(function (e) {
e.preventDefault();
var ac = $(this).attr('id');
$.each(data, function (index, value) {
$.each(value.units, function (bu, key) {
alert(key.name_units);
});
});
$(this).remove();
return false;
});
//////////////////////HERE*//////////////////////
$('body').click(function () {
$(".list_autobox_hotel p").hide().remove();
$('.auto_complete').val('');
$('.list_autobox_hotel').show().html('');
$('.list_autobox_hotel').css('display', 'none');
});
}
},
"error": function (x, y, z) {
// callback to run if an error occurs
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
});
UPDATE:
for bin i done it as: (but this don't work and there is still the same problem)
$('.list_autobox_hotel p').bind("click", function (e) {
e.preventDefault();
var ac = $(this).attr('id');
$.each(data, function (index, value) {
$.each(value.units, function (bu, key) {
alert(key.name_units);
});
});
$(this).remove();
return false;
});
i don't understand "filter the data depending on what item you click on"
and not know how is it!?
UPDATE 2
Did this is true? (but this don't work and there is alert: undefined
)
$('.list_autobox_hotel p').bind("click", function (e) {
e.preventDefault();
var ac = $(this).attr('id');
var ok = $.grep(data, function (e) {
return e.name == ac;
}).units;
alert(ok);
$(this).remove();
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的代码并不关心您单击哪个项目,它总是显示所有数据。
您应该在添加项目的循环内绑定单击事件,以便您可以使用每个项目的特定数据,或者根据您单击的项目过滤数据。
That is because your code doesn't care which item you click on, it always shows all the data anyway.
You should either bind the click event inside the loop where you add the items, so that you can use the specific data for each item, or filter the data depending on what item you click on.