-2

谁能帮我解决我的问题?我怎样才能使我的链接工作,它来自 ajax?

我尝试将一些链接直接写到文档中,并且效果很好,但“导入”的链接却没有。

JSFiddle

-> res/ajax/search.php

    <?php

$search = $_POST['search'];

if( $search != "" ) {

    $db = mysql_connect('localhost', 'root', '');
    mysql_select_db('invoice');

    $query = "SELECT * FROM profile";
    $result = mysql_query($query);

    $rows = mysql_num_rows($result);

    for( $a=1; $a <= $rows; $a++ ) {

        $query =  "SELECT * FROM profile WHERE full_name LIKE '%$search%' AND id='$a'";
        $result = mysql_query($query);

        $array = mysql_fetch_array($result);

        if($array != "") {

            echo "<a href='#' class='address'>" . $array['full_name'] . "</a><br />";

        }

    }

}


echo "<a href='#' class='address'>currywurst</a><br />";
echo "<a href='#' class='address'>bratwurst</a><br />";

?>
4

2 回答 2

1

利用:

$(document).on('click', '.address', function(){
  alert('something');
});

代替:

$('.address').click(function(){
  alert('something');
});
于 2013-09-06T13:26:31.507 回答
0

您应该尝试将处理程序绑定到 ajax 回调中的 ajax 加载内容。尝试这个:

$(document).ready(function(){

    $('input[name="search"]').keyup(function(){

        $.post('res/ajax/search.php',
        {
            search: $('input[name="search"]').val()
        },
        function(data) {
            $('#search-preview').html(data);

            // onclick moved to ajax callback function
            $('.address').click(function(){

                alert('something');

            });

        });

    });

});
于 2013-09-06T13:30:47.723 回答