dongshao1873
2016-03-08 11:51
浏览 37
已采纳

使用Ajax / jQuery自定义自动完成

I'm creating custom autocomplete with help of Ajax, jQuery and I have hard-coded one database which is created in PHP as I am getting correct output. But when I click on any text in the autocomplete box it does not get selected.

$(document).ready(function () {

    $("#search_Textbox").keyup(function () {
        var searchbox_Value = $("#search_Textbox").val();
        $("#serach_Result").show();
        if ($("#search_Textbox").val() == null || ($("#search_Textbox").val() == "")) {
            $("#serach_Result").hide();
        }

        $.ajax({
            url: "custom_database.php",
            type: "GET",
            data: {
                text_Value: searchbox_Value
            },
            success: function (server_Response) {
                $("#serach_Result").html(server_Response);
            }
        });
    });

    $('a').bind('click', function () {
        alert("yes");
        var achor_tag_text = $(this).val();
        alert(achor_tag_text);

        $("#search_Textbox").text(achor_tag_text);
    });
});

图片转代码服务由CSDN问答提供 功能建议

我在Ajax,jQuery的帮助下创建了自定义自动完成功能,并且我在一个数据库中进行了硬编码 PHP,因为我得到正确的输出。 但是,当我点击自动完成框中的任何文本时,它都不会被选中。

  $(document).ready(function(){
 
 $(“#  search_Textbox“)。keyup(function(){
 var searchbox_Value = $(”#search_Textbox“)。val(); 
 $(”#serach_Result“)。show(); 
 if($(”#search_Textbox  “).val()== null ||($(”#search_Textbox“)。val()==”“)){
 $(”#serach_Result“)。hide(); 
} 
 \  n $ .ajax({
 url:“custom_database.php”,
 type:“GET”,
 data:{
 text_Value:searchbox_Value 
},
 success:function(server_Response){
 $  (“#serach_Result”)。html(server_Response); 
} 
}); 
}); 
 
 $('a')。bind('click',function(){
 alert(  “是”); 
 var achor_tag_text = $(this).val(); 
 alert(achor_tag_text); 
 
 $(“#search_Textbox”)。text(achor_tag_text); 
}); 
  }); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpin2009 2016-03-08 12:04
    已采纳

    You could use .on() function it is more preferable from jquery 1.7

    $(document).on('click','a', function() {
           alert("yes");
           //do your stuff
           var achor_tag_text = $(this).val();
           alert(achor_tag_text);
           $("#search_Textbox").text(achor_tag_text);
    });
    

    but don't bind common element to click event give an class name to an element then bind your event with that class

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题