允许用户添加自己的文本字段

Allowing User to add their own text fields

我想知道我是否可以在我的项目中包含一个按钮,允许用户添加一个额外的文本字段供他们输入。

我有一个部分必须允许用户输入四个选项。但是,如果需要更多选项,他们应该能够添加更多文本框来实现这一点。

这将向您的表单添加一个输入元素

$("#form-id").append($("<input>", {name:"element-name", value:"Preset value"}))

表示"grab the element with the id 'form-id' and append an input-element, with the name and value this and that"。此操作应在单击按钮时执行,可以通过 运行 直接 "onclick" 或将其包装在函数调用中

已更新

你的意思是这样的?

HTML

<div>Add Text box</div>

JQUERY

$("div").click(function () {
    if (!$("input").length) {
        $("div").after("<input><br>");
    }
});

我创建了一个元素,当你点击它时它会添加一个输入

https://jsfiddle.net/kLe5kLc3/2/