简体   繁体   中英

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.

I call this function removeComment() which sends ID of div to be removed to server ( ID is also row id in database )

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

                    <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

            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. You should read more about what $(document).ready means. 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);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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