我正在为我的软件编写一个ajax实时搜索功能。有多个输入需要在每个表单上搜索ajax。一切都很好,除了我希望结果列表显示在悬停" p"我正在搜索的输入正下方的元素。现在我有一个" p" with class =" options"在每个输入下方以显示结果。当我输入时,结果显示在每个" p"中,即悬停在每个输入下。有没有办法只引用当前输入的子节点,还是我需要使用一个单独的函数显式引用每个输入所需的
?我在互联网上尝试了很多子选择器选项,但没有一个能够工作。代码如下。谢谢你的建议。
$(document).ready(function() {
//sets the chosen value into the input
$( ".options").on( "click", function(event) {
var item = $(event.target).text();
$('#acctname').val(item);//place chosed result into the acctname input
$('.options').hide();//hide result options after click
});
//On pressing a key in input field, this function will be called.
$(".textinput").keyup(function() {
var id = $(this).attr("id");
var val = $(this).val();
if (val === '') { //Validating, if "name" is empty.
$('div > p').html('').show();
}else {//If input is not empty.
$.ajax({
type: "POST",
url: "../model/ajax/livesearch.php",
data: {id: id, val: val},
success: function(html) {//place the result into the p element and set the proper css
$('div > p').html(html).show().css({"position":"absolute","z-index":"+1","cursor":"pointer","color":"black","background-color":"white"});
}
});
}
});
});
更新:这就是我的HTML表单的结构。
<form action= "../model/dataentry.php?formname=enter_deposit" method="POST" id="ajaxform" name="ajaxform">
<div class = "label"><p1>Revenue Account Name</p1></div><div class = "field"><input class = "textinput" type="input" name="acctname" id="acctname" value = "" autocomplete="off">
<p class = "options"></p></div>
<div class = "label"><p1>Revenue Account Number</p1></div><div class = "field"><input class = "textinput" type="input" name="acctno" id="acctno" value = "" autocomplete="off">
<p class = "options"></p></div>
<input class = "submit" name = "Submit" type = "button" id = "ajaxsubmit" value = "Submit" >
<input class = "submit" name = "Close" type = "button" id = "close" value = "Close" >
</div>
</form>
答案 0 :(得分:0)
这是你在找什么? https://jqueryui.com/autocomplete/
答案 1 :(得分:0)
如果有人好奇,我找到了解决问题的方法。通过为每个div分配类&#34;选项&#34;一个id等于其父亲的身份加上&#34; _opt&#34; (即&#34; acctname&#34;输入将有一个带有id&#34; acctname_opt&#34;)的子div,我可以在我的函数中动态编辑div id,将结果列表放在我想要的位置。这是我的新javascript代码。工作完美无瑕。感谢大家的帮助。
$(document).ready(function() {
//sets the chosen value into the input
$( ".options").on( "click", function(event) {
var item = $(event.target).text();
var clickedid = $(this).attr('id');
var targetid = clickedid.split('_'); //remove the '_opt' suffice to get the target id
$('#' + targetid).val(item);//place chosed result into the acctname input
$('.options').hide();//hide result options after click
});
//On pressing a key in input field, this function will be called.
$(".textinput").keyup(function() {
var id = $(this).attr("id");
var optid = id + '_opt'; //set the target element id
var val = $(this).val();
if (val === '') { //Validating, if "name" is empty.
$('#' + optid).html('').show();
}else {//If input is not empty.
$.ajax({
type: "POST",
url: "../model/ajax/livesearch.php",
data: {id: id, val: val},
success: function(html) {//place the result into the p element and set the proper css
$('#' + optid).html(html).show().css({"position":"absolute","z-index":"+1","cursor":"pointer","color":"black","background-color":"white"});
}
});
}
});
});