下拉菜单包含1-10之间的值列表。其目的是为从下拉菜单中选择的每个数字显示2个表单域。值3-10依此类推。
这是该表单的屏幕截图,其中显示了所有内容。enter image description here
发布于 2021-03-03 01:29:43
您可以使用document.getElementById(`serial-input-${index}`)获取串行元素
您可以使用document.getElementById(`serial-input-confirm-${index}`)获取序列确认元素
<p>Number of products</p>
<select id="select" onChange="create.input()"></select>
<div id="input"></div>const create = {
select: () => {
let select = document.getElementById('select')
// Create 1-10 number of products
new Array(10).fill(0).forEach((x, i) => {
let option = document.createElement('option')
option.innerHTML = (i+1)
option.value = (i+1)
select.appendChild(option)
})
},
input: () => {
let input = document.getElementById('input')
let select = document.getElementById('select')
console.log(select.value)
// Remove all child
input.innerHTML = ''
// Create the same number of input has you select in select ...
const size = parseInt(select.value, 10)
new Array(size).fill(0).forEach((x, i) => {
console.log(i)
let p_input = create.p_input(i)
p_input.forEach(x => input.appendChild(x))
})
},
p_input: index => {
let name = `erial number` + (index > 0 ? ' ' + (index+1) : '')
let serial_p = document.createElement('p')
serial_p.innerHTML = 'S' + name
serial_p.id = 'p-serial-' + index
serial_p.class = 'p-serial'
let serial_p_confirm = document.createElement('p')
serial_p_confirm.innerHTML = 'Confirm s' + name
serial_p_confirm.id = 'pc-serial-' + index
serial_p_confirm.class = 'pc-serial'
let serial_input = document.createElement('input')
serial_input.type = "text"
serial_input.id = "serial-input-" + index
serial_input.class = 'serial-input'
let serial_input_confirm = document.createElement('input')
serial_input_confirm.type = "text"
serial_input_confirm.id = "serial-input-confirm-" + index
serial_input_confirm.class = 'serial-input'
return [serial_p, serial_input, serial_p_confirm, serial_input_confirm]
}
}
create.select()
create.input()举个例子,这是我点击5得到的结果。


https://stackoverflow.com/questions/66442841
复制相似问题