doukun0888 2013-04-12 21:34
浏览 59
已采纳

完成搜索后,jQuery在悬停时加载数据

Problem

Hello, I have a simple search system using Jquery POST and PHP. After search, it will echo the information like this:

while($row = $search->fetch(PDO::FETCH_ASSOC)) {
echo 
'
<br /><br />
'.$row['result_name'].' <br /> Data: <a href="#" id="hover">Click to see</a><br /><br />
';

}

I want to make so when you hover the ID "hover" (after search), the java script will fill some div with the data, it will send another POST to get the data.

That's what I've done:

For the hover:

$("#hover").hover(function() {
  $.post("ajax_search.php?type=sidebar", { sidebar : seach.val() }, function(get_data) {
stop_load();
$("#sidebar").html(get_data);
     });
}); 

The PHP:

if (isset($_GET['type']) && $_GET['type'] == 'sidebar' && isset($_POST['sidebar'])) 
{
    $data = htmlentities(trim($_POST['search']));
    $similar = "'%$data%'";
    $search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
    $search->execute(); 

    while($row = $search->fetch(PDO::FETCH_ASSOC)) {
        echo 
        '
        <br /><br />
        '.$row['result_name'].' <br /> Data: '.$row['rsult_data'].'<br /><br />
        ';

    }
}

But nothing happens on hover!

Question

What have I done wrong? why doesn't it do anything on hover? Can I use IDs that have echoed after the script ran?

Additional

Full jquery code:

$(document).ready(function() {

    var search = $("#search");

        $("#bla").click(function() {
            if (search.val() != '') {
                load();
                $.post("ajax_search.php", { search : search.val()}, function(data) {
                    stop_load();
                    $(".result").html(data);
                });
            } 
            else 
            {
                $(".result").html('');
            }
            $("#hover").hover(function() {
                $.post("ajax_search.php?type=sidebar", { sidebar : seach.val() }, function(get_data) {
                    stop_load();
                    $("#sidebar").html(get_data);
                });
            });                 

function load() {
    $('.load').html('<img src="http://i.imgur.com/ZoI4x5I.gif" />');
}
function stop_load(){
    $('.load').html('');
}   
});

Full PHP code:

if (isset($_POST['search'])) 
{
    $data = htmlentities(trim($_POST['search']));
    $similar = "'%$data%'";
    $search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
    $search->execute();

    if ($search->rowCount() == 0) {
        echo 'You searched for <b>'.$data.'</b>, 0 results found.';
    } else {
        $results = $search->rowCount();
        echo 'You searched for <b>'.$data.'</b>, '.$results.' results found.';
        while($row = $search->fetch(PDO::FETCH_ASSOC)) {
            echo 
            '
            <br /><br />
            '.$row['result_name'].' <br /> Data: <a href="#" id="hover">Click to see</a><br /><br />
            ';

        }
    }
}
if (isset($_GET['type']) && $_GET['type'] == 'sidebar' && isset($_POST['sidebar'])) 
{
    $data = htmlentities(trim($_POST['search']));
    $similar = "'%$data%'";
    $search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
    $search->execute(); 

    while($row = $search->fetch(PDO::FETCH_ASSOC)) {
        echo 
        '
        <br /><br />
        '.$row['result_name'].' <br /> Data: '.$row['rsult_data'].'<br /><br />
        ';

    }
}

Thanks!

  • 写回答

1条回答 默认 最新

  • duanfanta6741 2013-04-12 21:38
    关注

    Try this .. as your <a href="#" id="hover"> being added dynamically

    $(document).on("mouseenter","#hover",function() {
         $.post("ajax_search.php?type=sidebar", { sidebar : seach.val() }, function(get_data){
                    stop_load();
                    $("#sidebar").html(get_data);
          });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度