如何同步两个SELECT元素

时间:2011-12-17 19:25:23

标签: javascript html

我想知道如何同步两个元素的值和文本。例如,

<select id="box1" onchange="sync();">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select id="box2">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

然后sync();会看起来像....

function sync()
{
box2.selected = box1.selected;
}

知道怎么做吗? 谢谢, 马修

4 个答案:

答案 0 :(得分:6)

一种可能的方法:

function sync(el1, el2) {
    // if there is no el1 argument we quit here:
    if (!el1) {
        return false;
    }
    else {
        // caching the value of el1:
        var val = el1.value;

        // caching a reference to the element with
        // with which we should be synchronising values:
        var syncWith = document.getElementById(el2);

        // caching the <option> elements of that <select>:
        var options = syncWith.getElementsByTagName('option');

        // iterating over those <option> elements:
        for (var i = 0, len = options.length; i < len; i++) {

            // if the value of the current <option> is equal
            // to the value of the changed <select> element's
            // selected value:
            if (options[i].value == val) {

                // we set the current <option> as
                // as selected:
                options[i].selected = true;
            }
        }
    }
}

// caching the <select> element whose change event should
// be reacted-to:
var selectToSync = document.getElementById('box1');

// binding the onchange event using an anonymous function:
selectToSync.onchange = function(){

    // calling the function:
    sync(this,'box2');
};

function sync(el1, el2) {
  if (!el1) {
    return false;
  } else {
    var val = el1.value;
    var syncWith = document.getElementById(el2);
    var options = syncWith.getElementsByTagName('option');
    for (var i = 0, len = options.length; i < len; i++) {
      if (options[i].value == val) {
        options[i].selected = true;
      }
    }
  }
}

var selectToSync = document.getElementById('box1');
selectToSync.onchange = function() {
  sync(this, 'box2');
};
<select id="box1">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<select id="box2">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

JS Fiddle demo

或者,有些修改和更新:

function sync() {

  // caching the changed element:
  let el = this;

  // retrieving the id of the element we should synchronise with
  // from the changed-element's data-syncwith custom attribute,
  // using document.getElementById() to retrieve that that element.
  document.getElementById( el.dataset.syncwith )
    // retrieving the <options of that element
    // and finding the <option> at the same index
    // as changed-element's selectedIndex (the index
    // of the selected <option> amongst the options
    // collection) and setting that <option> element's
    // selected property to true:
    .options[ el.selectedIndex ].selected = true;
}

// retrieving the element whose changes should be
// synchronised with another element:
var selectToSync = document.getElementById('box1');

// binding the snyc() function as the change event-handler:
selectToSync.addEventListener('change', sync);

function sync() {
  let el = this;
  document.getElementById(el.dataset.syncwith).options[el.selectedIndex].selected = true;
}

var selectToSync = document.getElementById('box1');
selectToSync.addEventListener('change', sync);
<select id="box1" data-syncwith="box2">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<select id="box2">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

JS Fiddle demo

请注意,此方法确实假设 - 并且需要 - <option>元素的顺序相同。

使用ES6方法(以及相同的data-syncwith自定义属性方法)更新订单无关的原始方法:

function sync() {
  // caching the changed element (since
  // we're using it twice):
  let el = this;

  // retrieving the id of the element to synchronise 'to' from 
  // the 'data-syncwith' custom attribute of the changed element,
  // and retrieving its <option> elements. Converting that
  // Array-like collection into an Array using Array.from():
  Array.from(document.getElementById(el.dataset.syncwith).options)
    // Iterating over the array of options using
    // Array.prototype.forEach(), and using an Arrow function to
    // pass the current <otpion> (as 'opt') setting that current
    // <option> element's selected property according to Boolean
    // returned by assessing whether the current option's value
    // is (exactly) equal to the value of the changed element:
    .forEach(opt => opt.selected = opt.value === el.value);
}

var selectToSync = document.getElementById('box1');
selectToSync.addEventListener('change', sync);

function sync() {
  let el = this;
  Array.from(document.getElementById(el.dataset.syncwith).options).forEach(opt => opt.selected = opt.value === el.value);
}

let selectToSync = document.getElementById('box1');
selectToSync.addEventListener('change', sync);
<select id="box1" data-syncwith="box2">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<select id="box2">
  <option value="1">One</option>
  <option value="3">Three</option>
  <option value="2">Two</option>
</select>

JS Fiddle demo

如果您查看代码段中的HTML,您会看到我在第二个<option>元素中切换了<select>个元素的位置,以证明<option>位置没有后一种方法很重要。

参考文献:

答案 1 :(得分:2)

在实际浏览器中,你不需要做太多......

<select id="box1" onchange="box2.value=this.value;">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>
<select id="box2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

Jsfiddle DEMO

答案 2 :(得分:1)

没有jQuery:

for (var i=0; i<document.getElementById('box1').options.length; i++)
  if (document.getElementById('box1').options[i].selected)
     for (var j=0; j<document.getElementById('box2').options.length; j++)
        if (document.getElementById('box1').options[i].value == document.getElementById('box2').options[j].value)
          document.getElementById('box2').options[j].selected = true;

答案 3 :(得分:0)

使用jQuery:

<select name="first" id="first" autocomplete="off">
    <option value="0">-- Select one option --</option>
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
    <option value="4">Fourth</option>
    <option value="5">Fifth</option>
    <option value="6">Sixth</option>
</select>

<select name="second" id="second" autocomplete="off">
    <option value="0">-- Select one option --</option>
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
    <option value="4">Fourth</option>
    <option value="5">Fifth</option>
    <option value="6">Sixth</option>
</select>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
jQuery(function($) {
    $('#first').on('change', function() {
        var sel = $('option:selected', this).val();
        $('#second option').filter(function(index, el) {
            return el.value == sel;
        }).prop('selected', true);
    });
});
</script>

注意: on method需要jQuery&gt; 1.7