user type london record fetch from data base and show below the input field
<input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" > //user type london
when user click on london than london add in input field but it cannot set in input field what jquery script require to select the value from p tag and show in input field
<script>
function autosearch(name){
//get data from database
$.ajax({
url:"ajax.php",
type:'post',
data: {name: name},
success:function(result){
$('.result').html(result);
}
});
}
</script>
ajax.php
<div style="border: 1px solid #ccc;width:100%;" >
<a href="javascript:void(0)">
</div>
<div>
<p><?php echo $data['cityName']; ?> </p> //london show here
</div>
</a>
</div>
最佳答案
如果我得到你的问题,那么下面是一个示例,让你清楚如何在输入中添加文本。请在下面结帐:
HTML:
<input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" >
<a href="javascript:void(0)" data-val="textToShow" onclick="addTextInInput($(this).data('val'))">
<div>
<p>asd </p>
</div>
</a>
JS:
function addTextInInput(txt){
//get data from database
$("#inputSuccess").val(txt)
}
将 data-val
中的 textToShow
替换为您的 PHP
代码并尝试。另外,在这里找到工作示例 https://jsbin.com/witilohinu/1/edit?html,js,console,output
请在评论中提供您的反馈。
关于javascript - 从 ajax 响应中选择文本并在用户单击项目时显示在输入字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109784/