简体   繁体   English

如何将多个值传递给jQuery中的函数

[英]how to pass multiple values to a function in jquery to

I have this list of comments that i want to remove from database using AJAX, and fadeout from current view with Javascript. 我有要使用AJAX从数据库中删除的注释列表,并希望使用Javascript从当前视图中淡出。

I call this function removeComment() which sends ID of div to be removed to server ( ID is also row id in database ) 我将此函数称为removeComment() ,该函数将要删除的div的ID发送到服务器(该ID也是数据库中的行ID)

The problem i have is, after i run the function first time, it stops working. 我的问题是,我第一次运行该函数后,它停止工作。

jquery code

function removeComment(PostId) {

    var commentid = 'com' + PostId;

    $(document).ready(function() {
        $(commentid).fadeToggle('slow');

        // send to php script
        $.ajax({
            type: 'POST',
            cache: false,
            url: 'actions/adminProcessor.php',
            data: 'action=removeComment' + '&PostId=' + PostId,
            success: function(done) {
                alert(done);
            }
        });
    }); // <-- Sorry, was missing a '}'
}

and below is the html of the comment list and how the functionis called 下面是注释列表的html以及该函数的调用方式

                    <div class="comments" id="com3">
      <label><admin>UD</admin></label><a href="Javascript:removeComment('1')">Remove</a>
      <span>17/09/12</span>
      <p>adfadfadfadf</p>
      </div>
    <div class="comments" id="com3">
      <label><admin>UD</admin></label><a href="Javascript:removeComment('3')">Remove</a>
      <span>17/09/12</span>
      <p>adfadfadfadf</p>
      </div>

please i would like to know where i got it wrong 请我想知道我在哪里弄错了

below is the php script 下面是PHP脚本

            if($action == "removeComment"){ 
            extract($_POST) ;
            $query = "DELETE FROM comments WHERE id = '$cId'" ;
            $result = mysql_query($query);
            }

You should not wrap your behavior into a $(document).ready function. 您不应将自己的行为包装到$(document).ready函数中。 You should read more about what $(document).ready means. 您应该详细了解$(document).ready含义。 This code should work now: 该代码现在应该可以工作:

function removeComment(PostId) {
    var commentid = 'com' + cid;
    var coms = document.getElementById(commentid);
    $(coms).fadeToggle('slow');
    $.ajax({
        type: 'POST',
        cache: false,
        url: 'actions/adminProcessor.php',
        data: 'action=removeComment' + '&PostId=' + PostId,
        success: function (done) {
            alert(done);
        }
    });
}

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

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