在另一个文本字段中的数字输入后,html表单中的其他文本字段

时间:2015-08-02 15:45:09

标签: text dynamic field

使用html表单使用php将数据发送到sql数据库。

我的麻烦在于,我为每个订单的项目数量设置了动态值,并且我试图避免为所有订单添加x个额外的文本字段。

更好的解决方案是在文本字段中输入一个值,然后在表单中显示相同数量的附加文本字段。

无论如何要做到这一点?

由于

1 个答案:

答案 0 :(得分:2)

行。因此,您需要在按下submit按钮之前根据用户的请求显示多个输入字段。我的第一种方法是在javascript中

让我们假设这种形式:

<form>
<p><input name="myInput1" /></p>
<button type="submit">submit</button>
</form>

您可以添加一个额外的按钮来添加新行:

<form>
<p><input name="myInput1" /></p>
<button type="button" onclick="addInput(this.form)">add input</button>
<button type="submit">submit</button>
</form>

...并且处理函数将是这样的:

<script type="text/javascript">
function addInput(form)
{
    // Create a new <p><input> node at the end of the form, throughput the DOM API:

    // Get the last <p> element of the form
    var paragraphs=form.getElementsByTagName("P")
    var lastParagraph=paragraphs[paragraphs.length-1]

    // Create a new <p> element with a <input> child:
    var newParagraph=document.createElement("P")
    var newInput=document.createElement("INPUT")
    // Name the <input> with a numeric suffix not to produce duplicates:
    newInput.name="myInput"+(1+paragraphs.length)
    newParagraph.appendChild(newInput)

    // Add the created <p> after the last existing <p> of the form:
    form.insertBefore(newParagraph, lastParagraph.nextSibling)
}
</script>

(请注意,所有呈现逻辑都是在客户端中执行(在HTML + javascript中),当表单最终提交时,服务器将只收到一对名称+值的集合。)