最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to close the box if click anywhere? - Stack Overflow

programmeradmin6浏览0评论

I have an code that the function is autosuggestion. This code works good. And I just want to ask You about how to close the box after I input text in the textbox. In my code when I try to click anywhere, it didn't close the box. So what I want to do is when I click anywhere so the box must be close.

Here is my jQuery:

$(document).ready(function() {
    $(".text_search").keyup(function() {
        var searchbox = $(this).val();
        var dataString = 'searchword=' + searchbox;
        if (searchbox == '') {} else {
            $.ajax({
                type: "POST",
                url: "search1.php",
                data: dataString,
                cache: false,
                success: function(html) {
                    $("#display").html(html).show();
                }
            });
        }
        return false;
    });
});​

HTML:

<div class="search">
<form method="get" action="search.php" autoplete="off">
<input class="text_search" type="text" name="q" id="q" value="Search people" onfocus="if (this.value == 'Search people') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search people';}">
</form>
</div>

<div id="display">
</div>

I have an code that the function is autosuggestion. This code works good. And I just want to ask You about how to close the box after I input text in the textbox. In my code when I try to click anywhere, it didn't close the box. So what I want to do is when I click anywhere so the box must be close.

Here is my jQuery:

$(document).ready(function() {
    $(".text_search").keyup(function() {
        var searchbox = $(this).val();
        var dataString = 'searchword=' + searchbox;
        if (searchbox == '') {} else {
            $.ajax({
                type: "POST",
                url: "search1.php",
                data: dataString,
                cache: false,
                success: function(html) {
                    $("#display").html(html).show();
                }
            });
        }
        return false;
    });
});​

HTML:

<div class="search">
<form method="get" action="search.php" autoplete="off">
<input class="text_search" type="text" name="q" id="q" value="Search people" onfocus="if (this.value == 'Search people') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search people';}">
</form>
</div>

<div id="display">
</div>
Share Improve this question edited Aug 23, 2012 at 7:27 Purag 17.1k4 gold badges56 silver badges78 bronze badges asked Aug 23, 2012 at 7:22 X-menX-men 691 silver badge5 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

Just add a event handler on the document body and close the box if it's open. Put that at the end of your body :

<script>
    $(window).ready(function(){
        $('body').click(function(){
            $("#display").hide();
        });
    });
</script>

EDIT :

If you want also to have the search launched again when you click the search box, change your script to this :

$(document).ready(function() {
    var launchSearch = function() {
        var searchbox = $(this).val();
        var dataString = 'searchword=' + searchbox;
        if (searchbox == '') {} else {
            $.ajax({
                type: "POST",
                url: "search1.php",
                data: dataString,
                cache: false,
                success: function(html) {
                    $("#display").html(html).show();
                }
            });
        }
        return false;
    };
    $(".text_search").keyup(launchSearch).click(launchSearch);
    $('body').click(function(){
        $("#display").hide();
    });
});​

Try this:

 $('body').click(function(){
        $("#display").hide();
    });
$(document).click(function(e) {
                  if($(e.target).parents("#display").length == 0 && 
                     $(e.target).attr("id") != "display") {
                         $("#display").hide();
                    }
               });

You must check if click is not in display div

Try this

$(document).ready(function(){
$('body').click(function(){
            $("#display").hide();
        });

$(".text_search").keyup(function()
{
var searchbox = $(this).val();
var dataString = 'searchword='+ searchbox;
if(searchbox=='')
{}
else
{
$.ajax({
type: "POST",
url: "search1.php",
data: dataString,
cache: false,
success: function(html)
{
$("#display").html(html).show();
}
});
}return false;
});
});
发布评论

评论列表(0)

  1. 暂无评论