0

「名前」、「住所」、「電話」などの値を持つ列ヘッダーとして一連のドロップダウンがあります。クライアント データが行に読み込まれ、ユーザーは各ドロップダウン列ヘッダーから正しいフィールドを選択してマッピングできます。データ。

What I want to do is allow only one instance of an option to be selected at a time. For example, if the user has selected 'name' from the first column dropdown, and then they change their minds and select 'name' from the second column dropdown, the first dropdown should revert to the default 'pick something' option value.

With the following, if a user sets a value on any column, it will reset all the other columns:

$('select').change(function(){
    $('select').not(this).val('0');
});

But I want to only reset the columns with $(this).val() selected. I've tried using this but it doesn't do anything:

$('select').change(function(){
    $('select option[value='+ $(this).val() +']').not(this).val('0');
});

This also failed:

$('select').change(function(){
    $('select').not(this).find('option[value='+ $(this).val() + ']').val('0');
});

Any ideas? Thank you!

Here's the html -- the number of dropdowns is variable depending on client data uploaded:

<select class="fun" id="field1" name="col[]">             
  <option value="0" selected>- Please select a field -</option>
  <option value="1">Name</option>
  <option value="2">Address</option>
  ...
</select>

<select class="fun" id="field2" name="col[]">             
  <option value="0" selected>- Please select a field -</option>
  <option value="1">Name</option>
  <option value="2">Address</option>
  ...
</select>
4

2 に答える 2

1

この解決策を試してください

    $('select').change(function () {
        var secondSelect = $('select').not(this);
        if (secondSelect[0].selectedIndex == this.selectedIndex) {
            secondSelect[0].selectedIndex = 0;
        }
    });
于 2013-10-31T06:07:50.660 に答える
0

試す

$('select').change(function(){
    $('select').not(this).has('option[value="'+  this.value + '"]:selected').val('0');
});

デモ:フィドル

于 2013-10-31T05:44:22.687 に答える