首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Squarespace中显示/隐藏表单域

在Squarespace中显示/隐藏表单域
EN

Stack Overflow用户
提问于 2021-03-03 00:02:56
回答 1查看 80关注 0票数 0

下拉菜单包含1-10之间的值列表。其目的是为从下拉菜单中选择的每个数字显示2个表单域。值3-10依此类推。

这是该表单的屏幕截图,其中显示了所有内容。enter image description here

EN

回答 1

Stack Overflow用户

发布于 2021-03-03 01:29:43

您可以使用document.getElementById(`serial-input-${index}`)获取串行元素

您可以使用document.getElementById(`serial-input-confirm-${index}`)获取序列确认元素

代码语言:javascript
运行
复制
<p>Number of products</p>
<select id="select" onChange="create.input()"></select>
<div id="input"></div>
代码语言:javascript
运行
复制
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得到的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66442841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档