简体   繁体   English

jquery评论系统on。(... click)问题

[英]jquery comment system on.(…click) issue

I'm having trouble keeping event handlers attached to future loaded selectors. 我将事件处理程序附加到未来加载的选择器上时遇到问题。 For example, when pull.php is loaded, the confirmdelete is no longer hidden, and also the click event handlers no longer exist. 例如,当加载pull.php时,不再隐藏confirmdelete,并且click事件处理程序也不再存在。 I am new to jquery and ajax. 我是jquery和ajax的新手。 Below is my code. 以下是我的代码。 $id= (int)strip_tags($_GET['id']); $ id =(int)strip_tags($ _ GET ['id']);

$(document).ready(function() { //make a comment delete js file eventually, just so we can reference the source
    $('.confirmdeletecomment').hide();
    $('.deletecomment').on("click", function(e){
        var cid = $(this).attr("id");
        $('a#c'+cid).show(500, function(){ //right now as call back
            $(this).on("click", function(e){
            var id = $(this).attr("id"); 
            var did = id.substring(1);  
            $.post(

                'deletecommentdata.php?cid='+did,

                function(data)
                {
                   $("#commentarea").load("pull.php?id=<? echo $id; ?>");
                   $("#comment").val("");
                   $('.confirmdeletecomment').hide();

                }

                )

                e.preventDefault();//so it doesn't interpret is as an anchor link

                });



                });

    e.preventDefault();//so it doesn't interpret is as an anchor link
    });


});

</script>

the below script is the php part: 下面的脚本是php部分:

<div id="commentarea">

<?

$query = mysql_query("SELECT users.user_id, users.username, users.miniavatar, comments.comment_id, comments.comment, comments.time_stamp FROM users INNER JOIN comments ON users.user_id=comments.products_users_user_id WHERE comments.products_products_id = '$id' ORDER BY comments.time_stamp DESC");

while($row2 = mysql_fetch_array($query)) {

?>
<div id='singlecomment'>

<hr class="comment" />
<table>
<col width="*" />
<col width="400" />
<col width="*" />    
<tr>
<td valign = "top" rowspan="2">
<a href="collection.php?profile=<? echo $row2['user_id']; ?>"><img src="<? echo $row2['miniavatar']; ?>" height="52" width="52" /></a> <br />
<?  
if ($user_id == $row2['user_id']) { 
    $cid = $row2['comment_id'];


    echo "<a id='$cid' class='deletecomment' title='Delete Post'>X</a> &nbsp";
    echo "<a id='c$cid' class='confirmdeletecomment'>confirm</a>";
}   
?>
</td>
<td valign="top">
<a class="blue" href="collection.php?profile=<? echo $row2['user_id']; ?>"> <? echo $row2['username']; ?> </a>
</td>
<td> 
<span class="date"><? echo date("F j, Y g:i a ", strtotime($row2['time_stamp'])); ?> </span>
</td>
<tr>
<td colspan="2">
<? echo stripslashes($row2['comment']); ?> <br/><br/>
</td>
</tr>
</table> 
</div>

What you need to do is use 'on' like it's meant. 你需要做的就是像它的意思一样使用'on'。 Right there all you're doing is binding a .click event to that element in the selector. 你正在做的就是将.click事件绑定到选择器中的该元素。

What you want is: 你想要的是:

$('body').on("click", ".deletecomment", function(e){

or something in place of 'body' that is a wrapper that can watch that level of the DOM for new elements to apply the event too. 或代替'body'的东西,它是一个包装器,可以观察DOM的级别,以便新元素也适用于该事件。

I am guessing the event got messed up since you are the passing the event handler from 我猜这个事件搞砸了,因为你正在传递事件处理程序

$('.deletecomment').on("click", function(e){

to

 $(this).on("click", function(e){

Change the event names, if you really want to handle them separately. 如果您真的想单独处理它们,请更改事件名称。 More like updating your second handler as these will do goo 更像是更新你的第二个处理程序,因为这些会做goo

$(this).on("click", function(event){

Since I dont have your markup structure, I am guessing, when you are loading pull.php to #commentarea , another element with class confirmdelete should have been loaded as well, thus making the code execution incomplete logically. 因为我没有你的标记结构,我猜测,当加载pull.php#commentarea ,带班另一个元素confirmdelete应该已经装为好,从而使代码执行不完整的逻辑。

Put, $(".confirmdelete").hide(); Put, $(".confirmdelete").hide(); right above e.preventDefault() to see if I am right. e.preventDefault()之上,看看我是否正确。

您应该查看事件委派系统http://api.jquery.com/on/

Try JQuery live. 试试JQuery直播。 more info It should be something like the following 更多信息它应该类似于以下内容

<script type="text/javascript">

$(document).ready(function() { //make a comment delete js file eventually, just so we can reference the source
    $('.confirmdeletecomment').hide();
    $('.deletecomment').live("click", function(){
        var cid = $(this).attr("id");
        $('a#c'+cid).show(500, function(){ //right now as call back
            $(this).on("click", "a", function(e){
            var id = $(this).attr("id"); 
            var did = id.substring(1);  
            $.post(

        'deletecommentdata.php?cid='+did,

        function(data)
        {
            $("#commentarea").load("pull.php?id=<? echo $id; ?>");
            $("#comment").val("");
            $('.confirmdeletecomment').hide();

        }

        )

            e.preventDefault();//so it doesn't interpret is as an anchor link

            });



        });

        e.preventDefault();//so it doesn't interpret is as an anchor link
        });


});

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM