如何在jquery中纠正循环内的数据

时间:2016-11-01 18:20:59

标签: php jquery json

我有以下代码。当我点击回复链接时,它会显示一个评论框。 我想要2分。 或者给我最好的工作方式。

  1. 当1个评论框打开时,其他必须隐藏。
  2. 当我点击发送按钮时,正确的值应该发送序列化值。
  3. 这些是代码 PHP代码

    <?PHP 
        for($i = 1; $i < 10; $i++)
        {
        ?>  
        <div>comments text etc etc...</div>
        <a href="" class="reply-comment"> Reply </a>
        <div class="reply-comment-form" style="display:none;">
            <form class="comment_form" method="post">     
                <textarea name="comment_box" rows="6" class="span10"></textarea> <br /><br />
                <input type="button" onClick="send_comment()" class="btn btn-primary" value="send" />
            </form> 
        </div>
    <?PHP 
       } 
    ?>
    

    Jquery代码

    <script> 
        $(function(){
            $('.reply-comment').on('click', function(e){ 
                e.preventDefault();
                $(this).next('.reply-comment-form').show();
            });
        });
    
    
        function send_comment()
        {   
            $.ajax({   
               type: "POST",
               data : $('.comment_form').serialize(),
               cache: false,  
               url: 'test.php',  
               success: function(data){
    
               }   
            });   
        }
    </script>
    

    test.php文件没有必要。我正在通过萤火虫检查价值观。 请帮我澄清一下这个问题。 或者给我最好的工作方式。

    我被困2天了。 感谢您宝贵的时间。

2 个答案:

答案 0 :(得分:1)

第一个

$('.reply-comment').on('click', function(e){

    e.preventDefault();

    // Hide others
    $(".reply-comment-form").hide();

    // Show the one which is required
    $(this).next('.reply-comment-form').show();

 });

第二,在表单上执行.on("submit"...,它将仅序列化正确的输入字段。

<强>更新

<form class="comment_form" method="post">     
      <textarea name="comment_box" rows="6" class="span10"></textarea> <br /><br />
      // Change type to submit and remove onclick
      <input type="submit" class="btn btn-primary" value="send" />
</form> 

<强> jQuery的:

$(".comment_form").on("submit", function(e){
    e.preventDefault(); // Here
    var _data = $(this).serialize();
    $.ajax({   
        type: "POST",
        data : _data,
        cache: false,  
        url: 'test.php',  
        success: function(data){
            console.log(data);
        }   
    });   

});

答案 1 :(得分:1)

我找到了解决方案。 @void帮助了我。

    $(".test").on("click", function(){

    var _data = $(this).parent().serialize();
    $.ajax({   
        type: "POST",
        data : _data,
        cache: false,  
        url: 'test.php',  
        success: function(data){

        }   
    }); 

});

谢谢!