简体   繁体   English

用与第一个下拉列表相同的值填充第二个下拉列表

[英]Fill second dropdown with the same value as the first dropdown

I have a dynamic number of dropdowns. 我有一个动态的下拉列表。 When a value is selected in the first dropdown, the value of the second and so on dropdowns will be the same with the first dropdown. 在第一个下拉菜单中选择一个值时, 第二个下拉菜单的值将与第一个下拉列表相同。 But the user has the option to change the value of the second and so on dropdowns , but not changing the first and other dropdown's value. 但是用户可以选择更改第二个下拉菜单的值, 以此类推 ,但不能更改第一个和其他下拉菜单的值。

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

How can I achieve this using javascript or jquery libraries? 如何使用javascriptjquery库实现此目的?

Use a change event and then alter the other dropdowns: 使用change事件,然后更改其他下拉菜单:

$("select[name=dropdown\\[\\]]").change(function() {
    var value = this.value;    
    $("select[name=dropdown\\[\\]]").not(this).val(value);
});

Demo: http://jsfiddle.net/awv14f6r/1/ 演示: http//jsfiddle.net/awv14f6r/1/

If you want to change them in order (first alters the second, etc) - use next along with this 如果您想按顺序更改它们(第一个更改第二个,依此类推)- thisnext一起使用

$(this).next("select").val(value);

You can attach a listener to the first select element: 您可以将侦听器附加到第一个select元素:

var selects = document.querySelectorAll('select[name="dropdown[]"]');

selects[0].addEventListener('change', function () {
  for (var i = 0; i < selects.length; i++) {
    selects[i].value = selects[0].value;
  }
});

When the value of only the first select changes, it updates the values of the other selects. 当只有第一个选择的值更改时,它将更新其他选择的值。 This way does not rely on any 3rd-party libraries. 这种方式不依赖任何第三方库。

Demo: http://jsfiddle.net/cuefb9ag/ 演示: http//jsfiddle.net/cuefb9ag/

var id = $('#DropDownID1 option:selected').val();
 var textValue = $('#DropDownID1 option:selected').text();
        $('#DropDownID2').prop('selectedIndex', 0);
        $('#DropDownID2').select2('data', {
            val: id,
            text: textValue
        });
        $('#DropDownID2 option:selected').val(id);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一个下拉列表中的值填写第二个下拉列表 - Fill second dropdown based on value from the first 如何使第二个下拉值与第一个下拉值相同? - How to make second dropdown value the same as the first dropdown value? 选中第一个下拉列表并在第二个下拉列表中显示相同的选定选项值 - The first dropdown selected and displays the same selected option value on the second dropdown 在数据库中更改第一个下拉列表中的第二个下拉列表中填充值 - Fill value in second dropdown on changing of first from database 我们有一个2下拉列表,在选择第一个下拉列表的多个值时具有相同的值,第二个下拉值是自动选择的 - We have a 2 dropdown list having same value on selection of multiple value of first dropdown second dropdown value is auto selected 如果在第一个下拉列表中选择了一个值(使用变量),则显示第二个下拉列表? - Display second dropdown list if a value is selected in the first dropdown (using variables)? 根据第一个下拉框的值预填充第二个下拉框 - prefill second dropdown box based on first dropdown box value 根据JSF中第一个下拉菜单的值来禁用第二个下拉菜单 - Disabling of second dropdown depending on the value of the first dropdown in JSF 在第一个下拉菜单更改时获取第二个下拉菜单的选项值-React - Grab option value of second dropdown on change of first dropdown - React 根据第一个下拉菜单的选定值填充第二个下拉菜单 - populate second dropdown based on selected value of first dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM