单选按钮如何更改<选项>? JS 中的下拉列表?

发布于 2025-01-19 01:06:34 字数 1934 浏览 6 评论 0 原文

我的目标是使无线电按钮更改&lt; select&gt; element(下拉列表)的&lt;

我已经能够解决反向行为(选择另一个&lt; option&gt; 会导致单选按钮的更改)。正如您在这里看到的那样:

function validate()
{
 var x = document.getElementById("location_select");
 var y = x.options[x.selectedIndex].value;
   {
    document.getElementById(y).checked = true;
   }
}
<select id="location_select" onchange="validate()">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>

<label>
  <input type="radio" id="berlin" name="location">
  <span>Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location">
  <span>Paris</span>
</label>

现在,我想生成反向行为。 所需的行为:更改无线电按钮还会在我的选择中更改&lt;

单选按钮ID与我的下拉选项的“值”相同。因此,可以将JS变量用于ID以及“值”。

基本上,这是我目前的进步:

function validate_2()
{
   {
     document.getElementById("location_select").selectedIndex = "1";
   }
}
<label>
  <input type="radio" id="berlin" name="location" onchange="validate_2()">
  <span class="title">Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location" onchange="validate_2()">
  <span class="title">Paris</span>
</label>

<select id="location_select">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>
As you can see, it's only static so far.
I'm open to better methods if this isn't a good one.

my goal is for radio buttons to change an <option> of a <select> element (drop-down list).

I was already able to solve the reverse behavior (selecting another <option> results in a change in a radio button). As you can see here:

function validate()
{
 var x = document.getElementById("location_select");
 var y = x.options[x.selectedIndex].value;
   {
    document.getElementById(y).checked = true;
   }
}
<select id="location_select" onchange="validate()">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>

<label>
  <input type="radio" id="berlin" name="location">
  <span>Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location">
  <span>Paris</span>
</label>

Now I would like to generate the reverse behavior.
Desired Behavior: Changing the radio button also changes the <option> in my selection.

The radio button ID is the same as the "value" of my drop-down options. So a JS variable can then be used for the ID as well as the "value".

Basically, this is my progress currently:

function validate_2()
{
   {
     document.getElementById("location_select").selectedIndex = "1";
   }
}
<label>
  <input type="radio" id="berlin" name="location" onchange="validate_2()">
  <span class="title">Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location" onchange="validate_2()">
  <span class="title">Paris</span>
</label>

<select id="location_select">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>

As you can see, it's only static so far.
I'm open to better methods if this isn't a good one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2025-01-26 01:06:34
function validate_2(city)
{
   
     document.getElementById("location_select").value = city;
   
}

function validate_1(){
  

    radio = document.getElementsByTagName('input')
    
    if(radio[0].checked)radio[1].checked = true
    else if (radio[1].checked)radio[0].checked = true
    


}
<label>
  <input type="radio" id="berlin" name="location" onchange="validate_2('berlin')" checked>
  <span class="title">Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location" onchange="validate_2('paris')">
  <span class="title">Paris</span>
</label>

<select id="location_select" onChange="validate_1()">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>

function validate_2(city)
{
   
     document.getElementById("location_select").value = city;
   
}

function validate_1(){
  

    radio = document.getElementsByTagName('input')
    
    if(radio[0].checked)radio[1].checked = true
    else if (radio[1].checked)radio[0].checked = true
    


}
<label>
  <input type="radio" id="berlin" name="location" onchange="validate_2('berlin')" checked>
  <span class="title">Berlin</span>
</label>

<label>
  <input type="radio" id="paris" name="location" onchange="validate_2('paris')">
  <span class="title">Paris</span>
</label>

<select id="location_select" onChange="validate_1()">
  <option value="berlin">Berlin</option>
  <option value="paris">Paris</option>
</select>

就像说晚安 2025-01-26 01:06:34

此功能可以通过更改 doady 输入

获取无线电输入的 ID ,可以更改&lt; select 的选定选项,该选项已更改并设置 id > value &lt; select&gt; 标签

版本1

function validate_2() {
    checkedVal = event.target.id;
    locationSelect = document.getElementById("location_select");
        
    locationSelect.value = checkedVal;
}

版本2

缩短到

function validate_2() {
    // ID becomes variable in JS
    location_select.value = event.target.id;
}

版本3

的属性可以使用 SelectedIndex 明确

function validate_2() {
    checkedVal = event.target.id;
    locationSelect = document.getElementById("location_select"); 
    indexToSelect = Array.from(locationSelect.options).findIndex(opt => opt.value == checkedVal);
    locationSelect.selectedIndex = indexToSelect;
}

This function can change selected option of <select> by changing radio inputs

Get the id of the radio input which was changed and set value attribute of <select> tag

Version 1

function validate_2() {
    checkedVal = event.target.id;
    locationSelect = document.getElementById("location_select");
        
    locationSelect.value = checkedVal;
}

Version 2

It can be shortened to

function validate_2() {
    // ID becomes variable in JS
    location_select.value = event.target.id;
}

Version 3

Using selectedIndex explicitly

function validate_2() {
    checkedVal = event.target.id;
    locationSelect = document.getElementById("location_select"); 
    indexToSelect = Array.from(locationSelect.options).findIndex(opt => opt.value == checkedVal);
    locationSelect.selectedIndex = indexToSelect;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文