如何在输入数据更改后更新下拉选项

huangapple go评论52阅读模式
英文:

How do I update dropdown options after input data change

问题

以下是您要翻译的内容:

I have dropdown list where options are filling with values inside js code. After click on submit button, I want my dropdown to be changed for updated values, but it will just add new values to already existed values.

My idea was in deleting all options before they are creating, so options with new values will be added from a scratch.

So I tried to receive the length of datalist, but something like var x = document.getElementById("browsers").options.length; from w3schools doesn't work.

In addition, I tried to receive all options with setting className for option and then get all options with const options = document.getElementsByClassName('datalist-option') and with this try I received all options, but then I can't iterate through them to do the following way:

const options = document.getElementsByClassName('datalist-option')

options.forEach(option => option.value = '')

which returns me error:

Uncaught TypeError: options.forEach is not a function
    at HTMLButtonElement.<anonymous>

Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const input = document.querySelector('#input')
const datalist = document.querySelector('#datalist')
const submitButton = document.querySelector('#submit-button')

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

values.forEach(value => {
    const option = document.createElement('option')
    option.value = value
    datalist.appendChild(option)
})

submitButton.addEventListener('click', (e) => {
    e.preventDefault()

    values.splice(0, 5)

    values.forEach(value => {
        const option = document.createElement('option')
        option.value = value
        datalist.appendChild(option)
    })
})

<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./test.js" defer></script>
</head>
<body>
    <input type="text" list="datalist" id="input">
    <datalist id="datalist"></datalist>

    <button type="submit" id="submit-button">Submit</button>
</body>
</html>
英文:

I have dropdown list where options are filling with values inside js code. After click on submit button, I want my dropdown to be changed for updated values, but it will just add new values to already existed values.

My idea was in deleting all options before they are creating, so options with new values will be added from a scratch.

So I tried to receive the length of datalist, but something like var x = document.getElementById(&quot;browsers&quot;).options.length; from w3schools doesn't work.

In addition, I tried to receive all options with setting className for option and then get all options with const options = document.getElementsByClassName(&#39;datalist-option&#39;) and with this try I received all options, but then I can't iterate through them to do the following way:

const options = document.getElementsByClassName(&#39;datalist-option&#39;)

options.forEach(option =&gt; option.value = &#39;&#39;)

which returns me error:

Uncaught TypeError: options.forEach is not a function
    at HTMLButtonElement.&lt;anonymous&gt;

Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.querySelector(&#39;#input&#39;)
const datalist = document.querySelector(&#39;#datalist&#39;)
const submitButton = document.querySelector(&#39;#submit-button&#39;)

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

values.forEach(value =&gt; {
      const option = document.createElement(&#39;option&#39;)
      option.value = value
      datalist.appendChild(option)
})

submitButton.addEventListener(&#39;click&#39;, (e) =&gt; {
      e.preventDefault()

      values.splice(0, 5)

      values.forEach(value =&gt; {
            const option = document.createElement(&#39;option&#39;)
            option.value = value
            datalist.appendChild(option)
      })
})

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Document&lt;/title&gt;
      &lt;script src=&quot;./test.js&quot; defer&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
      &lt;input type=&quot;text&quot; list=&quot;datalist&quot; id=&quot;input&quot;&gt;
      &lt;datalist id=&quot;datalist&quot;&gt;&lt;/datalist&gt;

      &lt;button type=&quot;submit&quot; id=&quot;submit-button&quot;&gt;Submit&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

这个问题实际上并不是发布给我来回答的,但实际上我找到了一个解决方案。

我遍历了datalist的子元素,它返回HTMLCollection。

for (let option of datalist.children)
    option.value = '';

现在代码按照它应该的方式工作。

const input = document.querySelector('#input')
const datalist = document.querySelector('#datalist')
const submitButton = document.querySelector('#submit-button')

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

values.forEach(value => {
    const option = document.createElement('option')
    option.value = value
    option.id = 'datalist-option'
    datalist.appendChild(option)
})

submitButton.addEventListener('click', (e) => {
    e.preventDefault()

    values.splice(0, 5)

    for (let option of datalist.children)
        option.value = '';

    values.forEach(value => {
        const option = document.createElement('option')
        option.value = value
        datalist.appendChild(option)
    })
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./test.js" defer></script>
</head>
<body>
    <input type="text" list="datalist" id="input">
    <datalist id="datalist"></datalist>

    <button type="submit" id="submit-button">Submit</button>
</body>
</html>

希望这可以帮助你。

英文:

Actually this question wasn't published just me to answer it, but actually I've found a solution.

I iterated through datalist children which returns HTMLCollection

for (let option of datalist.children)
            option.value = &#39;&#39;

And now code works as it has to

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.querySelector(&#39;#input&#39;)
const datalist = document.querySelector(&#39;#datalist&#39;)
const submitButton = document.querySelector(&#39;#submit-button&#39;)

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

values.forEach(value =&gt; {
      const option = document.createElement(&#39;option&#39;)
      option.value = value
      option.id = &#39;datalist-option&#39;
      datalist.appendChild(option)
})

submitButton.addEventListener(&#39;click&#39;, (e) =&gt; {
      e.preventDefault()

      values.splice(0, 5)

      for (let option of datalist.children)
            option.value = &#39;&#39;

      values.forEach(value =&gt; {
            const option = document.createElement(&#39;option&#39;)
            option.value = value
            datalist.appendChild(option)
      })
})

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Document&lt;/title&gt;
      &lt;script src=&quot;./test.js&quot; defer&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
      &lt;input type=&quot;text&quot; list=&quot;datalist&quot; id=&quot;input&quot;&gt;
      &lt;datalist id=&quot;datalist&quot;&gt;&lt;/datalist&gt;

      &lt;button type=&quot;submit&quot; id=&quot;submit-button&quot;&gt;Submit&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 03:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定