简体   繁体   English

jQuery Ajax选择器

[英]Jquery Ajax selectors

Im trying to make something like this http://demos.99points.info/facebook_wallpost_system/ which is a comment system. 我试图做这样的事情http://demos.99points.info/facebook_wallpost_system/这是一个评论系统。 I have the ajax code below except i don't know how to uniqely select textareas. 除了我不知道如何唯一选择textareas外,我下面有ajax代码。 The challenge is that the number of posts is variable so all of the posts need to be uniquely identified so that when the data is put into the database i know which post it relates to. 挑战在于帖子的数量是可变的,因此需要唯一地标识所有帖子,以便在将数据放入数据库时​​我知道它与哪个帖子相关。

JQUERY: JQUERY:

<script type='text/javascript'>
$('document').ready(function(){

$('.commentContainer').load('../writecomment.php');
        //commentContainer is a class so it applies to all of the textareas, but i need this selector to be unique

$('.submitCommentBox').click(function(){

            //these are the selectors that i can't get to work right
    var comment = $('').val();
    var postid = $('').val();


    $.post('../comment.php',
    {

    comment: comment,
    postid: postid,



    },
    function(response){

        $('#commentContainer').load('../writecomment.php');
        $('.commentBox').val('');

    }

    }

    return false;

}):

});

</script>

HTML/PHP HTML / PHP

<?php while ($row=mysql_fetch_assoc($query)){

echo"
<p name='singlePost'>$post[$f]</p>
<div id='commentContainer'></div>
<textarea class='commentBox'></textarea>
<input type='button' value='submit' class='submitCommentBox'>";
 }

basically the HTML/PHP generates for each post on the page as to create a textarea and subimt button for each post. 基本上,HTML / PHP为页面上的每个帖子生成,并为每个帖子创建一个textarea和subimt按钮。 therefore the user can comment on each post. 因此用户可以在每个帖子上发表评论。

Using the markup in the link you provided, I would do something like: 使用您提供的链接中的标记,我将执行以下操作:

var container  = $(this).closest('.friends_area');

var comment    = $(container).find('.commentbox').val();
var questionid = $(container).find('#hidden').val();
var answerid   = $(container).find('').val();

A more correct solution would be something like: 一个更正确的解决方案是:

HTML 的HTML

<div id="posting">
    <form method="post" action="">
        <input type="hidden" name="record_id" value="123" />
        <textarea name="comment"></textarea>
        <button type="submit">Comment</button>
    </form>

    ...
</div>

JS JS

$('#posting').on('submit', 'form', function(e) {

    e.preventDefault();

    var form = $(this).closest('form');

    $.post($(form).attr('action'), $(form).serialize(), function() {
        $('#commentContainer').load('../writecomment.php');
        $('.commentBox').val('');
    });
});

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

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