jQuery - 显示相反下拉列表的相反(反转选择)值

时间:2014-12-19 06:52:10

标签: jquery selectedvalue

我想显示选定下拉列表的相反值。

如果我从击球下拉菜单中选择“ A队”,那么保龄球队应该是“ B队,反之亦然。< / p>

HTML

Batting: 
<select class="battingTeam">
  <option>-- Select Team --</option>
  <option>Team A</option>
  <option>Team B</option>
</select>

<div id="bowlingTeam"></div>

的jQuery

$(document).ready(function(){
    $(".battingTeam").change(function(){
        $("#bowlingTeam").html( 'Bowling:' + this.options[this.selectedIndex].textContent);
     });                      
});


FIDDLE

3 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
    $(".battingTeam").change(function(){
        var batTeam = $(this).val();
        var bowlTeam = ( batTeam == "Team B" )?"Team A":"Team B";
        $('#bowlingTeam').html("Bowling:" +bowlTeam );
    });
});

检查小提琴:http://jsfiddle.net/hoja/24427kda/1/

答案 1 :(得分:0)

$(document).ready(function () {
    $(".battingTeam").change(function () {
        var value = (this.selectedIndex == 0) ? 0 : (this.selectedIndex == 1) ? 2 : 1;
        (value == 0) ? $("#bowlingTeam").hide() : $("#bowlingTeam").show().html('Bowling:' + this.options[value].textContent);

    });
});

<强> DEMO

答案 2 :(得分:0)

teams选项添加一个类,然后您不需要知道具体值

<option class="team">Team A</option>
<option  class="team">Team B</option>

JS

 $(".battingTeam").change(function () {       
    $("#bowlingTeam").html('Bowling:' + $(this).find('.team:not(:selected)').text());
});

DEMO