选择另一个下拉菜单中的选项时,如何选择特定的下拉菜单?

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

How to select a particular dropdown when an option is selected in other dropdown?

问题

以下是翻译好的内容:

两个下拉框具有类似的选项,但在选项中有一个变化。在第一个下拉框中存在“Audi”,但在第二个下拉框中不存在。

当所选的选项为所需选项时,这会选择每个下拉框中的相同选项。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars">选择一辆车:</label>
<select name="cars" id="cars1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<br><br>

<label for="cars">选择一辆车:</label>
<select name="cars" id="cars2">
  <option value="">请选择</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
</select>
$(document).on('change', '[name="cars"]', function(){
  $('[name="cars"]').val($(this).val());
})

当我选择“Audi”选项时,如何在保留当前功能的同时选择“请选择”选项。

英文:

2 dropdowns with similar options but 1 change in options. In the first dropdown Audi exists but not in second dropdown.

This selects the same option in each dropdowns when selected as required.

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;label for=&quot;cars&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars1&quot;&gt;
  &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
  &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
  &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
  &lt;option value=&quot;audi&quot;&gt;Audi&lt;/option&gt;
&lt;/select&gt;

&lt;br&gt;&lt;br&gt;

&lt;label for=&quot;cars&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars2&quot;&gt;
  &lt;option value=&quot;&quot;&gt;Please-Select&lt;/option&gt;
  &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
  &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
  &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
&lt;/select&gt;
$(document).on(&#39;change&#39;, &#39;[name=&quot;cars&quot;]&#39;, function(){
  $(&#39;[name=&quot;cars&quot;]&#39;).val( $(this).val() );
})

When I select the “Audi” option, how do I select “Please-Select” option while retaining the current functionality.

答案1

得分: 1

根据您的评论,您需要同时连接两个选择框。

我已为两个选择框添加了ID,以便更轻松地进行选择,并为目标选择框添加了一个属性。然后,您可以测试其目标选择框中的选定选项。如果存在,选择它。如果不存在,则选择默认选项。

通过这种针对现有匹配的测试方式,可以添加值,而无需担心第二个选择框或差异。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars1">Choose a car:</label>
<select name="cars1" id="cars1" data-target="cars2">
  <option value="">Please-Select</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<br><br>
<label for="cars2">Choose a car:</label>
<select name="cars2" id="cars2" data-target="cars1">
  <option value="">Please-Select</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="mclaren">McLaren</option>
</select>
$(document).ready(function(){
  $('select').on('change', function() {
    // 选定选择框的值
    var selected_select = $(this).attr('id');
    // 目标选择框的值
    var selected_target = $(this).attr('data-target');
    // 选项的值
    var selectedValue = $(this).val();

    // 检查目标值是否存在(true / false)
    var targetedValue = $('#' + selected_target + ' option[value="' + selectedValue + '"]').length > 0;

    $('#' + selected_select + ' option[value="' + selectedValue + '"]').prop('selected', true);

    // 如果值存在(true),则将其标记为选定,否则选择默认选项
    if(targetedValue === true) { 
      $('#' + selected_target + ' option[value="' + selectedValue + '"]').prop('selected', true);
    } else { 
      $('#' + selected_target + ' option[value=""]').prop('selected', true);
    }
  });
});

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

英文:

As per you comment, you need both selects to be linked in/out.

I have added IDs to both selects for a easier selection, and an attribute for the targeted select. Then you test the selected option in its targeted select. If it exists, select it. If not, select the default option.

This way of testing against an existing match will allow to add values without worrying about the second select or differences.

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

$(document).ready(function(){
  
$(&#39;select&#39;).on(&#39;change&#39;, function() {
  // the value of the selected select
  var selected_select = $(this).attr(&#39;id&#39;);
  //  the value of the targeted select
  var selected_target = $(this).attr(&#39;data-target&#39;);
  // the value of the option
  var selectedValue = $(this).val();

   // check if the targeted value exists (true / false)
   var targetedValue = $(&#39;#&#39;+selected_target+&#39; option[value=&#39;+selectedValue+&#39;]&#39;).length &gt; 0;

  $(&#39;#&#39;+selected_select+&#39; option[value=&#39;+selectedValue+&#39;]&#39;).prop(&#39;selected&#39;, true);

    // if value exists (true) then mark it as selected, else, pick the default option
    if(targetedValue === true) { 
      $(&#39;#&#39;+selected_target+&#39; option[value=&#39;+selectedValue+&#39;]&#39;).prop(&#39;selected&#39;, true);
    } else { 
      //$(&#39;#&#39;+selected_target+&#39; option[value=&quot;&quot;]&#39;).attr(&#39;selected&#39;,&#39;selected&#39;);
      $(&#39;#&#39;+selected_target+&#39; option[value=&quot;&quot;]&#39;).prop(&#39;selected&#39;, true);
    }
});

});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;label for=&quot;cars1&quot;&gt;Choose a car:&lt;/label&gt;
    &lt;select name=&quot;cars1&quot; id=&quot;cars1&quot; data-target=&quot;cars2&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please-Select&lt;/option&gt;
    &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
    &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
    &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
    &lt;option value=&quot;audi&quot;&gt;Audi&lt;/option&gt;
    &lt;/select&gt;
    &lt;br&gt;&lt;br&gt;
    &lt;label for=&quot;cars2&quot;&gt;Choose a car:&lt;/label&gt;
    &lt;select name=&quot;cars2&quot; id=&quot;cars2&quot; data-target=&quot;cars1&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please-Select&lt;/option&gt;
    &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
    &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
    &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
    &lt;option value=&quot;mclaren&quot;&gt;McLaren&lt;/option&gt;
    &lt;/select&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

更改了选择器,以通过ID选择。

将所选值保存在一个变量中,当值更改时,检查是否为audi,然后第二个select=&quot;cars&quot;上的选择值将为空值,即Please-select

注意:

  1. 在页面加载时添加了一个新的函数,以更改第一个select的值为第一个。删除此函数不会影响您的要求。随意修改它。
  2. for属性用于定位与label相关的输入,它必须是目标inputid,这是一个唯一的值,所以我将for属性的值更改为匹配inputsid
  3. 请记住,从技术上讲,在HTML表单中可能存在两个具有相同name属性的输入。但是,当提交表单时,这可能导致意外行为。

代码:

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

<!-- language: lang-js -->
$(document).ready(function(){
  var selectedValue = $('[id="cars1"]').val();
  $('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});

$(document).on('change', '[id="cars1"]', function(){
  var selectedValue = $(this).val();
  $('[id="cars1"]').val(selectedValue);
  $('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<label for="cars1">Choose a car:</label>
<select name="cars" id="cars1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<br><br>
<label for="cars1">Choose a car:</label>
<select name="cars" id="cars2">
  <option value="">Please-Select</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
</select>

<!-- end snippet -->

请注意,这是您提供的代码和注释的翻译。

英文:

Changed the selectors, to select by ID.

Saved the selected value in a variable, and on change, checking if the value is audi, then the selected value on the second select=&quot;cars&quot; will be the empty value, which is Please-select.

NOTES:

  1. Added a new function when the page is loaded, to change the value of the second select as the first one. Deleting this function won't affect your requirements. Feel free to modify it as you like.
  2. The for attribute is to target the input that related to label, and it has to be the id of the target input which is a unique value, so I changed the for attributes values to match the id's of the inputs.
&lt;label for=&quot;cars1&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars1&quot;&gt;
...
&lt;/select&gt;

&lt;label for=&quot;cars2&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars2&quot;&gt;
...
&lt;/select&gt;
  1. Keep in mind, that technically, it is possible to have two inputs with the same name attribute in an HTML form. However, it can lead to unexpected behavior when submitting the form.

The Code:

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

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

$(document).ready(function(){
  var selectedValue = $(&#39;[id=&quot;cars1&quot;]&#39;).val();
  $(&#39;[id=&quot;cars2&quot;]&#39;).val(selectedValue === &#39;audi&#39; ? &#39;&#39; : selectedValue);
});

$(document).on(&#39;change&#39;, &#39;[id=&quot;cars1&quot;]&#39;, function(){
  var selectedValue = $(this).val();
  $(&#39;[id=&quot;cars1&quot;]&#39;).val(selectedValue);
  $(&#39;[id=&quot;cars2&quot;]&#39;).val(selectedValue === &#39;audi&#39; ? &#39;&#39; : selectedValue);
});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;label for=&quot;cars1&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars1&quot;&gt;
  &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
  &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
  &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
  &lt;option value=&quot;audi&quot;&gt;Audi&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;&lt;br&gt;
&lt;label for=&quot;cars1&quot;&gt;Choose a car:&lt;/label&gt;
&lt;select name=&quot;cars&quot; id=&quot;cars2&quot;&gt;

  &lt;option value=&quot;&quot;&gt;Please-Select&lt;/option&gt;
  &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
  &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
  &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
  &lt;/select&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 01:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248836.html
匿名

发表评论

匿名网友

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

确定