1

in a view im repating the same form and posting it via ajax, my concern is the ajax is working for 1st form but not working from second form, below is the code im using.

<form action="http://localhost/comment" method="post" accept-charset="utf-8">
<input type="text" name="comment_text" value="" id="comment_text" size="35" class="comment_text">
<input type="submit" id="post_comment" name="post_comment" value="submit comment" class="post_comment" >
</form>
<form action="http://localhost/comment" method="post" accept-charset="utf-8">
<input type="text" name="comment_text" value="" id="comment_text" size="35" class="comment_text">
<input type="submit" id="post_comment" name="post_comment" value="submit comment" class="post_comment" >
</form>
<form action="http://localhost/comment" method="post" accept-charset="utf-8">
<input type="text" name="comment_text" value="" id="comment_text" size="35" class="comment_text">
<input type="submit" id="post_comment" name="post_comment" value="submit comment" class="post_comment" >
</form>
<form action="http://localhost/comment" method="post" accept-charset="utf-8">
<input type="text" name="comment_text" value="" id="comment_text" size="35" class="comment_text">
<input type="submit" id="post_comment" name="post_comment" value="submit comment" class="post_comment" >
</form>

<script type="text/javascript">
$('.post_comment').click(function() {
  var form_data = {
    csrfsecurity: $("input[name=csrfsecurity]").val(),
    post_text: $('.comment_text').val()    
  };

  $.ajax({
    url: "<?php echo site_url('/comment'); ?>",
    type: 'POST',
    data: form_data,
    success: function(response){
      $(".home_user_feeds").html("markUpCreatedUsingResponseFromServer");
    }
  });
  return false;
});
</script>
4

1 回答 1

0

你的问题是post_text: $('.comment_text', this).val()哪个会给你第一个 selected 的值.comment,你想要的是.comment当前形式的值。尝试

<script type="text/javascript">
$('.post_comment').click(function() {
  var form_data = {
    csrfsecurity: $("input[name=csrfsecurity]").val(),
    post_text: $(this).closest('form').find('.comment_text').val()    
  };

  $.ajax({
    url: "<?php echo site_url('/comment'); ?>",
    type: 'POST',
    data: form_data,
    success: function(response){
      $(".home_user_feeds").html("markUpCreatedUsingResponseFromServer");
    }
  });
  return false;
});
</script>

元素 ID 也应该是唯一的。

于 2013-03-04T18:41:29.713 回答