weixin_33739523 2015-08-16 08:54 采纳率: 0%
浏览 25

PHP Ajax实时搜索框

I am trying to make a PHP Ajax live search Box, so far is working good but it seems that I am facing two problems:

  • The first one is that when data is showing on the screen, I want it to disappear if I move the mouse outside the search box.
  • The second one is related to CSS, I want the position of data results to be just under my search box, now is floating right.

Here the code:

<div class="widget-container widget_search">
        <span class="adds"></span>
        <form action="" id="searchform" method="POST">
            <p>
                <input type="text" id="search" placeholder="Chercher" size="30" name="search">
                <button type="submit" id="searchsubmit"></button>
            </p>
        </form><!--/ #searchform-->

        <div id="livesearch" style=" width:auto; height:auto; margin:auto;  position: absolute;"></div>
    </div><!--/ .widget-container-->

JS:

$(document).ready(function(){
    $("#search").keyup(function(){
        var valor = $("#search").val();
            $.ajax({
                type: "POST",
                url: "/auto/search/",
                data: {word:valor},
                success: function(res) {
                    $('#livesearch').html(res);
                }
            });
    });
});
  • 写回答

2条回答 默认 最新

  • lrony* 2015-08-16 09:29
    关注

    Let'suppose the data container has an id, like "myid". Then, you can hide it like this:

    document.getElementById('myid').style.display = "none";
    

    You can make it visible like this:

    document.getElementById('myid').style.display = "block";
    

    To make this general, you can do something like this:

    function changeDisplay(element, display) {
        element.style.display = display;
    }
    

    You can store the data container like this:

    var dataContainer = document.getElementById("myid");
    

    Now, you want to hide it when the mouse leaves. So, you need to set the onmouseout of your search box to a function like this:

    function left() {
        changeDisplay(dataContainer, "none");
    }
    

    But you probably want to make it reappear when you hover to the element. So you need to set the onmouseover event to a function like this:

    function entered() {
        changeDisplay(dataContainer, "block");
    }
    

    As about positioning the results, you might consider adding a <br> after the search tag and then position it to the left.

    评论

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大