简体   繁体   English

使用javascript添加表单元素 - 他们不会提交

[英]adding form elements with javascript - they won't submit

This is similar to the problem here: .../questions/1386228/add-form-element-dynamically-using-javascript-not-submitting but I don't know what his answer was besides rewriting from scratch. 这类似于这里的问题:... / questions / 1386228 / add-form-element-dynamic-using-javascript-not-submitting但我不知道他的答案是什么,除了从头开始重写。

Here's my problem: 这是我的问题:

Form elements added dynamically to the page do not appear in the $_POST array. 动态添加到页面的表单元素不会出现在$ _POST数组中。 I'm doing this same method several other instances on the page and it works fine, i'm hoping there is just a typo or something obvious I am missing. 我正在页面上的其他几个实例使用相同的方法,它工作正常,我希望只有一个错字或一些明显我遗漏的东西。

HTML HTML

<tr valign="top">
<td></td>
<td align="right">Staff Comments:</td>
<td></td>
<td>

<select name="staffcomment0[scstid]">
<option value="">Choose</option>
<option value="10">Abs</option>
<option value="4">Andy</option>
</select> says:<br>
<TEXTAREA NAME="staffcomment0[desc]" ROWS="3" COLS="55" WRAP tabindex="99">Brilliant stuff</TEXTAREA><br><br>

<select name="staffcomment1[scstid]">
<option value="">Choose</option>
<option value="10">Abs</option>
<option value="4">Andy</option>
</select> says:<br>
<TEXTAREA NAME="staffcomment1[desc]" ROWS="3" COLS="55" WRAP tabindex="99">Great!</TEXTAREA><br><br>

<SPAN ID="staffcomments"></SPAN>

<A HREF="javascript:addComment()">add another comment</A></td>
</tr>

Javascript: 使用Javascript:

var commentNo = 2;

function addComment() {

outcHTML = "<select name='staffcomment" + commentNo + "[scstid]'><option value=''>Choose</option>";
outcHTML += "<option value='10'>Abs</option>";
outcHTML += "<option value='4'>Andy</option>";
outcHTML += "</select> says:<br><TEXTAREA NAME='staffcomment" + commentNo + "[desc]' ROWS='3' COLS='55' WRAP tabindex='99'></TEXTAREA><br><br>";

var newdiv = document.createElement("div");
newdiv.innerHTML = outcHTML;
var container = document.getElementById("staffcomments");
container.appendChild(newdiv);

commentNo++;
}

Added HTML but realised it is too long to be displayed properly! 添加了HTML,但意识到它太长而无法正常显示!

当你在Firebug中观察DOM时,新元素实际上是在<form>元素中吗?

Your HTML is munged, so it's hard to know exactly what's going on, but I'm fairly certain the answer is that you're adding the elements as straight HTML, not as DOM objects. 你的HTML很简陋,所以很难确切地知道发生了什么,但我相当肯定答案是你将元素添加为直接HTML,而不是DOM对象。

In other words, instead of setting the innerHTML of newdiv, you have to append new objects to it, such as: 换句话说,不必设置newdiv的innerHTML,而是必须向其添加新对象,例如:

var newInput = document.createElement("input");
newInput.type="text";
newInput.name="inputName";
newdiv.appendChild(newInput);

(Code typed off the top of my head, so apologies for any typos...) (代码键入了我的头顶,所以为任何错别字道歉......)

And as another commenter noted, you have to also make sure that you append the new objects inside the form object in the DOM. 正如另一位评论者指出的那样,您还必须确保将新对象附加到DOM中的表单对象中。

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

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