当另一个选择元素的选项更改时,使用Javascript更改另一个元素的选项值

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

Changing Option Value Of Another Element With Javascript When Another Select Element's Option Is Changed

问题

I have translated the provided code snippet into Chinese:

我正在尝试创建一个基本的翻译器,其中脚本将在土耳其语和英语之间进行翻译,但在我尝试创建程序时,我遇到了一个问题。我有2个选择元素。

当具有id“languages”的选择元素的选项为英语时,我希望具有id“languagesT”的选择元素为土耳其语,当我在“languages”中选择土耳其语时,也是如此。因此,基本上我希望它们相反,这是我到目前为止使用JavaScript所做的:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
const translating = document.getElementById("languages")
const translator = document.getElementById("languagesT")

translating.addEventListener("change", () => {
    if (translating.value === "turkish") {
      translator.value = "english";
    }
    else if (translating.value === "english") {
      translator.value = "turkish";
    }
  }
);

<!-- 语言: lang-html -->
<select name="languages" id="languages">
    <option value="english">英语</option>
    <option value="turkish">土耳其语</option>
</select>
<select name="languagesT" id="languagesT">
    <option value="turkish">土耳其语</option>
    <option value="english">英语</option>
</select>

<!-- 结束代码片段 -->

请注意,这是您提供的代码的翻译部分。

英文:

I am trying to create a basic translator where the script will translate between Turkish and English, but while I was trying to create the program, I got stuck at a point. I have 2 select elements.

When the select element with the id of "languages" option is English I want the select element with the id of "languagesT" to be Turkish and the same when I select Turkish in languages. So basically I want them to be opposite, and this is what I have done so far with javascript:

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

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

const translating = document.getElementById(&quot;languages&quot;)
const translator = document.getElementById(&quot;languagesT&quot;)

translating.addEventListener(&quot;change&quot;, () =&gt; {
    if (translating.value === &quot;turkish&quot;) {
      translator.value = &quot;english&quot;;
    }
    else if (translating.value === &quot;english&quot;) {
      translator.value = &quot;turkish&quot;;
    }
  }
);

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

&lt;select name=&quot;languages&quot; id=&quot;languages&quot;&gt;
    &lt;option value=&quot;english&quot;&gt;English&lt;/option&gt;
    &lt;option value=&quot;turkish&quot;&gt;Turkish&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;languagesT&quot; id=&quot;languagesT&quot;&gt;
    &lt;option value=&quot;turkish&quot;&gt;Turkish&lt;/option&gt;
    &lt;option value=&quot;english&quot;&gt;English&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

答案1

得分: 0

你可以修正你的拼写或委托

这里我们不关心语言是什么

<div id="languages">
  <select name="language1" id="en_tr">
    <option value="english" selected>English</option>
    <option value="turkish">Turkish</option>
  </select>
  <select name="language2" id="tr_en">
    <option value="turkish" selected>Turkish</option>
    <option value="english">English</option>
  </select>
</div>
document.getElementById("languages").addEventListener("change", (e) => {
  const tgt = e.target;
  const value = tgt.value;
  e.currentTarget.querySelectorAll("select").forEach(sel => {
    if (tgt !== sel) sel.selectedIndex = tgt.selectedIndex === 0 ? 0 : 1;
  });
});
英文:

You could fix your spelling or delegate

Here we do not care what the languages are

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

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

document.getElementById(&quot;languages&quot;).addEventListener(&quot;change&quot;, (e) =&gt; {
  const tgt = e.target;
  const value = tgt.value;
  e.currentTarget.querySelectorAll(&quot;select&quot;).forEach(sel =&gt; {
    if (tgt!==sel) sel.selectedIndex = tgt.selectedIndex === 0 ? 0 : 1;
  });  
});

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

&lt;div id=&quot;languages&quot;&gt;
&lt;select name=&quot;language1&quot; id=&quot;en_tr&quot;&gt;
    &lt;option value=&quot;english&quot; selected&gt;English&lt;/option&gt;
    &lt;option value=&quot;turkish&quot;&gt;Turkish&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;language2&quot; id=&quot;tr_en&quot;&gt;
    &lt;option value=&quot;turkish&quot;selected&gt;Turkish&lt;/option&gt;
    &lt;option value=&quot;english&quot;&gt;English&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

Your javascript code looks correct; there's a typo in your HTML code in the second option value of the second select element.

<select name="languagesT" id="languagesT">
    <option value="turkish">Turkish</option>
    <option value="english">English</option>
</select>

it should be

<select name="languagesT" id="languagesT">
    <option value="turkish">Turkish</option>
    <option value="english">English</option>
</select>
英文:

Your javascript code looks correct; there's a typo in your HTML code in the second option value of the second select element.

&lt;select name=&quot;languagesT&quot; id=&quot;languagesT&quot;&gt;
    &lt;option value=&quot;turkish&quot;&gt;Turkish&lt;/option&gt;
    &lt;option value=&quot;englisih&quot;&gt;English&lt;/option&gt;
&lt;/select&gt;

it should be

&lt;select name=&quot;languagesT&quot; id=&quot;languagesT&quot;&gt;
    &lt;option value=&quot;turkish&quot;&gt;Turkish&lt;/option&gt;
    &lt;option value=&quot;english&quot;&gt;English&lt;/option&gt;
&lt;/select&gt;

huangapple
  • 本文由 发表于 2023年6月12日 01:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451583.html
  • html
  • javascript
  • select
  • web
  • web-developer-toolbar

如何将MySQL表列作为变量放入 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定