Javascript getElementById和多个表单

Javascript getElementById和多个表单,javascript,jquery,forms,getelementbyid,multiple-forms,Javascript,Jquery,Forms,Getelementbyid,Multiple Forms,我在许多表格中都遇到了这种情况,例如,我向您展示了其中的一些表格: <form method="POST" enctype="multipart/form-data"> <textarea name="content" id="content" class="form-control" required=""></textarea> <input name="code" id="code" type="hidden" value="118022

我在许多表格中都遇到了这种情况,例如,我向您展示了其中的一些表格:

<form method="POST" enctype="multipart/form-data">  
<textarea name="content" id="content" class="form-control" required=""></textarea>
   <input name="code" id="code" type="hidden" value="1180224194">
   <input name="postId" id="postId" type="hidden" value="167">  
   <button class="btn btn-commenta">Say something</button>
</form>
<form method="POST" enctype="multipart/form-data">
   <textarea name="content" id="content" class="form-control" required=""></textarea>
   <input name="code" id="code" type="hidden" value="95959622661">
   <input name="postId" id="postId" type="hidden" value="144">  
   <button class="btn btn-commenta">Say something</button>
</form>
我有一些javascript,如下所示:

<script type="text/javascript">
   $("form").submit(function (e) {
        e.preventDefault();
        var comment = document.getElementById("content").value;
        var postId = document.getElementById("postId").value;
        var code = document.getElementById("code").value;
        if(comment && postId && code){
        $.ajax({
          type: 'POST',
          ...SOME AJAX
        });
        }
        else {
            console.log("error");
            console.log(comment);
            console.log(postId);
            console.log(code);
        }
        return false;
    });
</script>
当我有一个表单或使用第一个表单时,一切都正常,但当我尝试以不同于第一个表单的形式获取输入值时,会出现错误,并且字段为空


如何让脚本选择提交表单的getElementById值?我尝试了这个方法,但控制台告诉我无法读取未定义的属性“getElementById”。

尝试对不同的元素使用不同的id。Id应始终是唯一的。如果要对多个元素使用相同的名称(例如样式),请使用类

我建议将您的ID更改为:

<form method="POST" enctype="multipart/form-data">  
<textarea name="content" id="content1" class="form-control" required="">
</textarea>
    <input name="code" id="code1" type="hidden" value="1180224194">
    <input name="postId" id="postId1" type="hidden" value="167">  
    `enter code here`<button class="btn btn-commenta">Say something</button>
</form>
<form method="POST" enctype="multipart/form-data">
    <textarea name="content" id="content2" class="form-control" required="">
    </textarea>
    <input name="code" id="code2" type="hidden" value="95959622661">
    <input name="postId" id="postId2" type="hidden" value="144">  
    <button class="btn btn-commenta">Say something</button>
</form>
这些学校解释说:

id属性为HTML元素指定一个唯一的id值 在HTML文档中必须是唯一的

id属性最常用于指向样式表中的样式, 并通过JavaScript通过htmldom对元素进行操作 特定id

id属性在同一文档中应该是唯一的,请使用公共类,如:

<form method="POST" enctype="multipart/form-data">
  <textarea name="content" class="form-control content" required=""></textarea>
  <input name="code" class="code" type="hidden" value="1180224194">
  <input name="postId" class="postId" type="hidden" value="167">
  <button class="btn btn-commenta">Say something</button>
</form>
希望这有帮助

$form.submitFunction{ e、 防止违约; var comment=$.content,this.val; var postId=$.postId,this.val; var code=$.code,this.val; console.logcomment; console.logposted; console.logcode; 如果注释&&postId&&code{ console.logajax查询; }否则{ console.logerror; } 返回false; }; 说点什么 说点什么
您永远不应该对不同的元素使用相同的id。可能的重复项看起来是一个很好的解决方案,我刚刚遇到一些问题,即为这些变量输入分配新值,引用它们的类。我怎么做?
var comment = $(".content", this).val();
var postId = $(".postId", this).val();
var code = $(".code", this).val();