jQuery .load()函数只被调用一次

时间:2012-03-23 11:05:08

标签: jquery load click

#####已解决#####  我有一个javaScript刷新setInterval(“...”,1000);在导致错误的源代码中。非常感谢你的帮助!

html

<div class="stackwrapper" id="user1"></div>
<div class="stackwrapper" id="user2"></div>
<div class="userdrawings"></div>

javascript

$('.stackwrapper').click(function(e){
var id=$(this).attr('id');
$('.userdrawings').load('loadSession.php?user='+id).fadeIn("slow");
});

不知怎的,它只能一次工作,只有第一次点击stackwrapper时,当我点击第二个时,该功能不再被触发。

5 个答案:

答案 0 :(得分:1)

好的,现在我明白了,这是因为你正在进行ajax调用。这是 a link ,可以回答您的问题。

答案 1 :(得分:0)

尝试把它放在

$(document).ready(function(){
//place the above code here.
});

答案 2 :(得分:0)

无法使用此自包含示例重现问题

<?php
if ( isset($_GET['user']) ) {
    echo '<span>user=', htmlspecialchars($_GET['user']), '</span>';
    die;
}
?>
<html>
    <head>
        <title>...</title>
        <style type="text/css">
            .stackwrapper { width:100px; height: 100px; margin: 5px;  background-color: red }
        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('.stackwrapper').click(function(e) {
                    var id=$(this).attr('id');
                    $('.userdrawings').load('?user='+id).fadeIn("slow");
                });
            });
        </script>
    </head>
    <body>
        <div class="stackwrapper" id="user1"></div>
        <div class="stackwrapper" id="user2"></div>
        <div class="userdrawings"></div>
    </body>
</html>

你可能想要使用javascript调试器,例如包含在firebug中并检查错误。

答案 3 :(得分:0)

您可以尝试将其放在文档级别:

$(document).on('click', '.stackwrapper', function(e) {
    var id = $(this).attr('id');
    $('.userdrawings').load('loadSession.php?user=' + id).fadeIn("slow");
});

答案 4 :(得分:0)

这很奇怪,虽然它在我的电脑上运行良好。每次点击都会改变。 这是我的代码

HTML

<div class="stackwapper" id="user1">user1</div>
<div class="stackwapper" id="user2">user2</div>
<div class="userdrawings"></div>

JS

$(document).ready(function(){
$(".stackwapper").click(function(e) {
       var id = $(this).attr('id');
       $(".userdrawings").load("user_session.php?user="+id).fadeIn("slow");
    });
});

user_session.php

$user=$_GET["user"];
echo "hello " . $user;
相关问题