添加到 HTML 表单而不丢失 Javascript 中当前表单输入信息

发布于 2024-08-19 23:42:53 字数 341 浏览 3 评论 0 原文

我有一个下拉菜单,它根据所选的选择构建一个表单。因此,如果有人选择“foobar”,它会显示一个文本字段,如果他们选择“cheese”,它会显示单选按钮。然后,用户可以在这些表单中输入数据。唯一的问题是,当他们添加新的表单元素时,所有其余信息都会被删除。我目前使用以下内容添加到表单:

document.getElementById('theform_div').innerHTML = 
    document.getElementById('theform_div').innerHTML + 'this is the new stuff';

如何让它保留表单中输入的内容并将新字段添加到末尾?

I have a drop down which builds a form based of the selections that are selected. So, if someone selects 'foobar', it displays a text field, if they choose 'cheese', it displays radio buttons. The user can then enter data into these forms as they go along. The only problem is that when they add a new form element, all the rest of the information is erased. Im currently using the following to do add to the form:

document.getElementById('theform_div').innerHTML = 
    document.getElementById('theform_div').innerHTML + 'this is the new stuff';

How can I get it to keep whatever has be enetered in the form and also add the new field to the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奢华的一滴泪 2024-08-26 23:42:53

设置 innerHTML 会破坏元素的内容并从 HTML 重建它。

您需要构建一个单独的 DOM 树并通过调用 appendChild 添加它。

例如:

var container = document.createElement("div");
container.innerHTML = "...";
document.getElementById("theform_div").appendChild(container);   

使用 jQuery 可以更容易地做到这一点。

Setting innerHTML destroys the contents of the element and rebuilds it from the HTML.

You need to build a separate DOM tree and add it by calling appendChild.

For example:

var container = document.createElement("div");
container.innerHTML = "...";
document.getElementById("theform_div").appendChild(container);   

This is much easier to do using jQuery.

做个少女永远怀春 2024-08-26 23:42:53

第一步:

将 jQuery 添加到标头:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>

第二步:

将数据附加(而不是替换)到 DIV,如下所示:

$("#theform_div").append("your_new_html_goes_here");

Step One:

Add jQuery to your headers:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>

Step Two:

Append, don't replace, data to your DIV like this:

$("#theform_div").append("your_new_html_goes_here");
つ低調成傷 2024-08-26 23:42:53

不要使用 innerHTML 创建表单元素。使用innerHTML,您将使用新的 HTML 覆盖旧的 HTML,这将重新创建所有元素。相反,您需要使用 DOM 来创建和附加元素。

示例

function addRadioElement()
{
    var frm = document.getElementById("form_container");
    var newEl = document.createElement("input");
    newEl.type = "radio";
    newEl.name = "foo";
    newEl.value = "bar";
    frm.appendChild(newEl);        
}

Don't use innerHTML to create the form elements. With innerHTML you're overwriting the old HTML with new HTML which will recreate all the elements. Instead you need to use the DOM to create and append the elements.

EXAMPLE

function addRadioElement()
{
    var frm = document.getElementById("form_container");
    var newEl = document.createElement("input");
    newEl.type = "radio";
    newEl.name = "foo";
    newEl.value = "bar";
    frm.appendChild(newEl);        
}
墨落成白 2024-08-26 23:42:53

不使用框架(如 jQuery、Dojo、YUI)的最正确方法是:

var text = document.createTextNode('The text you want to write');
var div = document.createElement('div');
div.appendChild(text);

document.getElementById('theform_div').appendChild(div);

innerHTML,虽然大多数浏览器都支持,但不符合标准,因此不能保证正常工作。

The most correct way to do it without using a framework (like jQuery, Dojo, YUI) is:

var text = document.createTextNode('The text you want to write');
var div = document.createElement('div');
div.appendChild(text);

document.getElementById('theform_div').appendChild(div);

innerHTML, although supported by most browsers, is not standard compliant and - therefore, not guaranteed to work.

二手情话 2024-08-26 23:42:53

我建议使用 jQuery 及其 追加函数。

I would suggest using jQuery and its append function.

庆幸我还是我 2024-08-26 23:42:53

如果您想使用 HTML 字符串生成元素,如 innerHTML 那样,一个不错的选择是 insertAdjacentHTML 函数,及其 position 参数:

let newElement = "<div>this is the new stuff</div>";
document.getElementById("theform_div")
.insertAdjacentHTML("beforeend", newElement);

如您所见 Mozilla 的文档,除了更快之外,这还解决了您遇到的问题因为该函数不会重新解析父元素:

insertAdjacentHTML() 方法不会重新解析正在使用它的元素,因此它不会破坏该元素内的现有元素。这避免了额外的序列化步骤,使其比直接的innerHTML操作快得多。

但是,如果您只想添加纯文本,而不是 HTML 代码,那么有一个类似的函数 专为名为 插入AdjacentText

let newText = "this is the new stuff";
document.getElementById("theform_div")
.insertAdjacentText("beforeend", newText);

If you want to generate the element using an HTML string, as innerHTML does, a great option is the insertAdjacentHTML function, with it's position parameter:

let newElement = "<div>this is the new stuff</div>";
document.getElementById("theform_div")
.insertAdjacentHTML("beforeend", newElement);

As you can see from Mozilla's documentation, besides being faster, this solves the problem you had because the function doesn't reparse the parent element:

The insertAdjacentHTML() method does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than direct innerHTML manipulation.

However, if you just want to add plain text, not HTML code, then there is a similar function made just for that called insertAdjacentText:

let newText = "this is the new stuff";
document.getElementById("theform_div")
.insertAdjacentText("beforeend", newText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文