简体   繁体   English

将选项标签附加在不同选择标签中选择的值

[英]append option tag with value which are selected in different select tag

I have Three Select Tags Two with player names 我有三个选择标签,两个带有球员姓名

<select >
<option value="plarer1">plarer1</option>
<option value="plarer2">plarer2</option>
</select>
<select >
<option value="plarer3">plarer3</option>
<option value="plarer4">plarer4</option>
</select>

and the third one is empty and hidden when I will select the value from both select tags they may both appear with value and name in the third select tag 当我将从两个选择标签中选择值时,第三个是空的且隐藏的,它们可能在第三个选择标签中都带有值和名称

One way to do this is using javascript in the browser: 一种方法是在浏览器中使用javascript:

First make function to create a simple HTML select element that contains a number of options specified by their title. 首先使用make函数创建一个简单的HTML select元素,其中包含由其标题指定的多个选项。 This is pretty straight forward: 这很简单:

function createDropdownWithTitles(...optionTitles) {
  const dropdown = document.createElement('select')
  const defaultOption = new Option('Choose', null, true, true)
  defaultOption.disabled = true
  dropdown.add(defaultOption)
  for (const title of optionTitles) {
    const option = new Option(title, title)
    dropdown.add(option)
  }
  return dropdown
}

Then make a function that creates a dynamic dropdown. 然后创建一个创建动态下拉列表的函数。 This is an HTML select element that will dynamically retrieve its options from the selected options of other dropdown boxes. 这是一个HTML select元素,将从其他下拉框的选定选项中动态检索其选项。

This becomes a bit more complex. 这变得更加复杂。 There are two things we need to do: 我们需要做两件事:

  • Keep track of the dropdowns that are filled in already: we call this filledInDropdowns in our code. 跟踪已经填写的下拉列表:我们在代码filledInDropdowns称为filledInDropdowns Once all these dropdowns are filled in we can make the dynamic dropdown visible. 填写完所有这些下拉菜单后,我们就可以使动态下拉菜单可见。

  • Keep track of the contents of newly filled in dropdowns. 跟踪新填写的下拉列表的内容。 We should reflect these contents in our dynamic dropdown. 我们应该在动态下拉菜单中反映这些内容。

Here is it in code 这是代码

function createDropdownBasedOn(...otherDropdowns) {
  const dropdown = document.createElement('select')
  dropdown.style.visibility = 'hidden'
  const filledInDropdowns = new Array(otherDropdowns.length)
  for (const [index, dropdown] of otherDropdowns.entries()) {
    dropdown.addEventListener('change', event => fillIn(event.currentTarget, index))
    filledInDropdowns[index] = false
  }
  function fillIn(dependency, index) {
    filledInDropdowns[index] = true
    dropdown[index] = new Option(dependency.value, dependency.value)
    for (const filledIn of filledInDropdowns) {
      if (filledIn === false) return
    }
    dropdown.style.visibility = 'visible'
  }
  return dropdown
}

And then to wrap up create some sample dropdowns: 最后,创建一些示例下拉列表:

const dropdownA = createDropdownWithTitles('Player 1', 'Player 2')
const dropdownB = createDropdownWithTitles('Player 3', 'Player 4')

document.body.appendChild(dropdownA)
document.body.appendChild(dropdownB)
document.body.appendChild(createDropdownBasedOn(dropdownA, dropdownB))

Check out a working example: 查看一个工作示例:

 function createDropdownWithTitles(...optionTitles) { const dropdown = document.createElement('select') const defaultOption = new Option('Choose', null, true, true) defaultOption.disabled = true dropdown.add(defaultOption) for (const title of optionTitles) { const option = new Option(title, title) dropdown.add(option) } return dropdown } function createDropdownBasedOn(...otherDropdowns) { const dropdown = document.createElement('select') dropdown.style.visibility = 'hidden' const filledInDropdowns = new Array(otherDropdowns.length) for (const [index, dropdown] of otherDropdowns.entries()) { dropdown.addEventListener('change', event => fillIn(event.currentTarget, index)) filledInDropdowns[index] = false } function fillIn(dependency, index) { filledInDropdowns[index] = true dropdown[index] = new Option(dependency.value, dependency.value) for (const filledIn of filledInDropdowns) { if (filledIn === false) return } dropdown.style.visibility = 'visible' } return dropdown } const dropdownA = createDropdownWithTitles('Player 1', 'Player 2') const dropdownB = createDropdownWithTitles('Player 3', 'Player 4') document.body.appendChild(dropdownA) document.body.appendChild(dropdownB) document.body.appendChild(createDropdownBasedOn(dropdownA, dropdownB)) 

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

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