简体   繁体   English

你如何在 html 表单中包含一个对象?

[英]How do you include an object in a html form?

I'm trying to add an object to a html form.我正在尝试将对象添加到 html 表单。 Object will look like this:对象将如下所示:

let bestilling = {
    btnUndersider1:      500,
    btnUndersider2:      750,
    btnUndersider3:     1500,
    btnUndersider4:     4500,
}

I've tried having an invisible input field, and then using the #id.value and also #id.innerHTML, however neither of theese worked out.我试过有一个不可见的输入字段,然后使用 #id.value 和 #id.innerHTML,但是这些都没有解决。 It's important that the object is invisible to the customers.重要的是该对象对客户是不可见的。 How do i do this?我该怎么做呢? Thanks.谢谢。

You can just loop through your object.您可以循环遍历您的对象。 This creates a new <span> and <input> for each, with a new line.这会为每个创建一个新的<span><input> ,并换行。 If you want to make any of them invisible, before appendChild , just apply e.style.visibility = "hidden" or "e.style.display = none" , depending on the layout you want.如果您想让它们中的任何一个不可见,在appendChild之前,只需应用e.style.visibility = "hidden""e.style.display = none" ,具体取决于您想要的布局。

let bestilling = {
    btnUndersider1:      500,
    btnUndersider2:      750,
    btnUndersider3:     1500,
    btnUndersider4:     4500,
}

for (x=0; x<Object.keys(bestilling).length; x++) {
    e = document.createElement('span');
    e.innerText = Object.keys(bestilling)[x] + " - ";
    document.body.appendChild(e);

    e = document.createElement('input');
    e.value = bestilling[Object.keys(bestilling)[x]];
    document.body.appendChild(e);
    document.body.appendChild(document.createElement('br'));
}

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

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