【发布时间】:2017-10-02 14:46:01
【问题描述】:
我正在做一个项目,我需要在调用函数时更改某个类的所有选择输入的值。问题是第一次加载dom时一些select输入不存在,它们是通过Javascript动态创建的。
该函数适用于选择页面加载时存在的所有选择输入,但不适用于动态添加到 dom 的任何选择输入。
我了解与 .on 的事件绑定,但在这种特殊情况下,我并不想关闭诸如单击之类的事件。我想在调用这个函数时获取这个类的所有元素。
这是我用来创建新选择元素的代码。
var this_cell = document.getElementById('produce_batch_line_td['+x+']');
var new_select = document.createElement("select");
new_select.name = "produce_batch_line["+x+"]";
new_select.id = "produce_batch_line["+x+"]";
new_select.className = "produce_batch_line["+invoice_number+"]";
this_cell.appendChild(new_select);
var new_option = document.createElement("option");
new_option.text = "test";
new_option.value = "test";
new_select.add(new_option);
这是我用来选择这个类的所有选择元素的代码。
function SetDetailValue(invoice_number, obj) {
$(".produce_batch_line\\["+invoice_number+"\\]").each(function() {
this.value = obj.value;
});
}
如何将此类的项目动态添加到 dom 中?
【问题讨论】:
-
Prolly 需要查看您的 html 标记来回答这个问题。
标签: javascript jquery html dom