Ajax不适用于同一页面上的多个表单

Ajax不适用于同一页面上的多个表单,ajax,codeigniter,jquery,Ajax,Codeigniter,Jquery,在我重新绘制同一个表单并通过ajax发布的视图中,我关心的是ajax正在为第一个表单工作,而不是从第二个表单工作,下面是我使用的代码 <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"

在我重新绘制同一个表单并通过ajax发布的视图中,我关心的是ajax正在为第一个表单工作,而不是从第二个表单工作,下面是我使用的代码

<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>

$('.post_comment')。单击(函数(){
变量形式_数据={
csrfsecurity:$(“输入[name=csrfsecurity]”)。val(),
post_text:$('.comment_text').val()
};
$.ajax({
url:“”,
键入:“POST”,
数据:表格数据,
成功:功能(响应){
$(“.home\u user\u feeds”).html(“markUpCreatedUsingResponseFromServer”);
}
});
返回false;
});

您的问题是
post_text:$('.comment_text',this).val()
将为您提供第一个选定的
.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>

$('.post_comment')。单击(函数(){
变量形式_数据={
csrfsecurity:$(“输入[name=csrfsecurity]”)。val(),
post_text:$(this).closest('form').find('comment_text').val()
};
$.ajax({
url:“”,
键入:“POST”,
数据:表格数据,
成功:功能(响应){
$(“.home\u user\u feeds”).html(“markUpCreatedUsingResponseFromServer”);
}
});
返回false;
});

元素ID也应该是唯一的。

ID必须是唯一的。但是,您可以将一个类应用于多个元素。将
id=“post\u comment”
更改为
class=“post\u comment”
。他也在使用类。id无效,但这不会影响代码,因为他没有使用它们。我怀疑这是一个委托问题,他总是从第一张表格而不是提交的表格中获得价值。@Kevin B没错,说得好。