У меня есть поиск, похожий на Google, который выпадает с результатами, пока пользователь печатает. В идеале я хотел бы, чтобы пользователь щелкнул по одному из этих результатов, и значение попадет в окно поиска. Однако, когда я нажимаю на результаты, ничего не происходит.
HTML:
<input type='text' id='topicInput' name='topic' autocomplete='off' />
<div id='tagResult'></div> //this is the dropdown
Jquery:
$('#topicInput').keyup(function(){
var topic = $(this).val();
if (topic==''){
$('#tagResult').css("display" , "none");
}
else{
//$('div').click(function(){
//$('#tagResult').css("display" , "none");
//});
$('#tagResult').css("display" , "block");
$.post('../topic.php' , {topic: topic} , function(response){
$('#tagResult').html(response);
});
}
});
//the above code is working properly
$('.topicResult').click(function(){
alert(1); //this is just a test, but it never shows up
});
Итак, когда я нажимаю на .topicResult, ничего не происходит. Предупреждение должно появиться. Я убедился, что topic.php действительно возвращает div с классом topicResult.