我正在使用HTML表单使用php将数据发送到sql数据库。
我的麻烦是,我有一个动态的商品数量值,该数量随每个订单而变化,并且试图避免必须为所有订单添加x数量的额外文本字段。
更好的解决方案是在文本字段中输入一个值,然后使相同数量的其他文本字段出现在表单中。
反正有做到这一点吗?
谢谢
好的。因此,您想在按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)执行的,并且当最终提交表单时,服务器将只收到名称+值对的集合。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句