2014-04-10 51 views
0

我试图填充并显示select如果前12个下拉列表中有一个值。如果两个选择值已被选择运行功能

我似乎无法得到我的if语句正确,但任何人都可以看到我可能做错了什么?

$('select').on('change',function(){ 

if($('.player1') != null || $('.player2') != null){ 

    // If both players have been selected, show the winner dropdown and populate it with the value from player1 and player 2. 
    $('.winner').show(); 

} 

}); 

http://jsfiddle.net/kbPLn/1/

+0

你的例子中的下拉菜单怎么没有价值? – laaposto

+0

我希望该下拉菜单的值可以是前面2个下拉菜单中选择的值 – Liam

回答

0

你永远不会为空的情况。(因为你不是检查.VAL ......而且,他们总是有一个VAL)更新你的HTML +你的选择在这里:

http://jsfiddle.net/kbPLn/5/

if($('.player1>option:selected').val() != "No value" && $('.player2>option:selected').val() != "No value"){ 

更新 http://jsfiddle.net/kbPLn/9/

 var first = $('.player1>option:selected').val(), 
     second =$('.player2>option:selected').val(); 

     $('.winner').find('option:first').val(first).html(first); 
     $('.winner').find('option:not(:first)').val(second).html(second); 
     $('.winner').show(); 
+0

Ahh谢谢@JFit,是否有可能从第2个下拉,然后填充第二选择与那些? – Liam

+0

是的 - 1秒病了更新 –

+0

@Liam那你在找什么? http://jsfiddle.net/kbPLn/9/ –

0

你需要使用&&检查时否定值

1

一个jQuery对象永远等于null。你可以做的是检查集合的length属性。此外,它看起来像你想要&&而不是||

if ($('.player1>option:selected').length && $('.player2>option:selected').length) { 

编辑:你需要做的是检查它是否有一个选择的子选项。

+0

应该是'$('。player1> option:selected')。length' *您在那里检查的元素总是有长度= 1。 –

+0

...............................看看他的jsfiddle ......... –

+0

糟糕。 :P我会解决这个问题。 – Scimonster

0
Try this. This may help you: 
$('select').on('change', function() { 
      $('select[name="winner"]').html(''); 
      if ($('.player1') != null || $('.player2') != null) { 

       // If both players have been selected, show the winner dropdown and populate it with the value from player1 and player 2. 
       $('.winner').show(); 

       //$('.winner'). 
      } 
      $('select[name="winner"]').append('<option value="' + $(".player1").val() + '">' + $(".player1").val() + '</option>'); 
      $('select[name="winner"]').append('<option value="' + $(".player2").val() + '">' + $(".player2").val() + '</option>'); 
     });