如何使用JavaScript更改HTML选择元素中的选择内容?

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

How to change what is selected in a HTML select element using JavaScript?

问题

// 通过 JavaScript 选择一个元素
document.querySelector('.slimselect').value = "Mr.";
英文:

I want to make select an element using Javascript in this HTML-select element

<div class="vb-fg-field-input ">
  <select class="slimselect required" name="tx_vbformgenerator_vb_formgenerator[Anrede]" tabindex="-1" data-ssid="ss-2638" style="display: none;" data-ddg-inputtype="unknown">
    <option value=" ">not specified</option>
    <option value="1">Mrs.</option>
    <option value="2">Mr.</option>
    <option value="5">Mx.</option>
  </select>
  <div class="ss-2638 ss-main slimselect required" style="">
    <div class="ss-single-selected">
      <span class="placeholder">not specified</span>
      <span class="ss-deselect ss-hide">x</span>
      <span class="ss-arrow">
        <span class="arrow-down"></span>
      </span>
    </div>
    <div class="ss-content">
      <div class="ss-search ss-hide">
        <input readonly="" type="search" placeholder="Search" tabindex="0" aria-label="Search">
      </div>
      <div class="ss-list">
        <div class="ss-option ss-disabled ss-option-selected" data-id="84420967">not specified</div>
        <div class="ss-option" data-id="78147293">Mrs.</div>
        <div class="ss-option" data-id="65028532">Mr.</div>
        <div class="ss-option" data-id="13286904">Mx.</div>
      </div>
    </div>
  </div>
</div>

I read this question, https://stackoverflow.com/questions/10911526/how-do-i-programatically-select-an-html-option-using-javascript , especially this answer https://stackoverflow.com/a/41969836/1802826 , but none of the suggested solutions seem to work for me.

I have tried this JS

document.getElementsByClassName('slimselect')[0].value = "Mr."

which returns

<- 'Mr.'

.

document.getElementsByClassName('slimselect')[0].querySelectorAll('option')[2].selected = "Mr."

which returns

<- 'Mr.'

document.getElementsByClassName('slimselect')[0].querySelector('option')[2]

which returns

<- Undefined

and multiple variations of these.

How do I, using JavaScript (preferably a "oneliner"), change what is selected here, either based on index (1-4), the value attribute (1, 2, 5) or the element string (not specified, Mrs., Mr. or Mx.)?

Note that I want to do it from the browser console, because I will later on wrap in an AppleScript that controls a browser. I am not gonna use it in a webpage.

答案1

得分: 1

控件是一个第三方脚本,所以仅仅通过DOM选择来设置值/索引不会通知JavaScript发生了变化。当您更新值/selectedIndex 时,JavaScript 不会触发事件,因此您需要触发事件以使其捕捉到您所做的更改。您可以使用 dispatchEvent 来实现这一点。

const sel = document.querySelector('.slimselect');
sel.selectedIndex = 2;
sel.dispatchEvent(new Event('change'));

或者您可以使用它们的API来设置它:

const sel = document.querySelector('.slimselect');
sel.slim.setSelected(['2']);
英文:

The control is a third party script so setting the value/index with just the DOM select is not going to notify the JavaScript something changed. JavaScript does not trigger events when you update the value/selectedIndex so you need to trigger events for it to pick up the change you made. You do that with dispathEvent.

const sel = document.querySelector('.slimselect');
sel.selectedIndex = 2;
sel.dispatchEvent(new Event('change'));

or you use their api to set it

const sel = document.querySelector('.slimselect');
sel.slim.setSelected(['2']);

答案2

得分: 0

你可以找到指定文本的<option>,然后设置<select>元素的selectedIndex

const textToSelect = 'Mr.';
const select = document.querySelector('.slimselect');
select.selectedIndex = [...select.options].findIndex(o => o.text === textToSelect);

如果你已经知道要选择的选项的索引,可以直接设置它:

document.querySelector('.slimselect').selectedIndex = 2;

如果你知道要选择的选项的值:

document.querySelector('.slimselect').value = '2';
// 文本为“Mr.”的<option>的值
英文:

You can find the <option> with the specified text, then set the <select> element's selectedIndex.

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

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

const textToSelect = &#39;Mr.&#39;;
const select = document.querySelector(&#39;.slimselect&#39;);
select.selectedIndex = [...select.options].findIndex(o =&gt; o.text === textToSelect);

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

&lt;select class=&quot;slimselect required&quot;&gt;
  &lt;option value=&quot; &quot;&gt;not specified&lt;/option&gt;
  &lt;option value=&quot;1&quot;&gt;Mrs.&lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt;Mr.&lt;/option&gt;
  &lt;option value=&quot;5&quot;&gt;Mx.&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

If you already know the index of the option to choose, you can directly set it:

document.querySelector(&#39;.slimselect&#39;).selectedIndex = 2;

If you know the value of the option to choose:

document.querySelector(&#39;.slimselect&#39;).value = &#39;2&#39;;
// value of &lt;option&gt; with text of &quot;Mr.&quot;

</details>



huangapple
  • 本文由 发表于 2023年7月14日 00:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681394.html
匿名

发表评论

匿名网友

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

确定