双重if值语句jQuery

时间:2017-04-28 11:54:49

标签: javascript jquery html

是否可以使用jQuery获得2个if语句? 现在就像你选择20 Fruit然后会显示一个div。

这是我的HTML:

<select name="bestemming" id="CoachTravel" class="form-control select" >
<option value="10 Glasses" >10 Glasses</option>
<option value="20 Fruit" >20 Fruit</option>
<option value="30 Cars" >30 Cars</option>
</select>

我的jQuery:

$('#venetie').hide(); 

 $('#CoachTravel').change(function(){
        if($('#CoachTravel').val() == '20 Fruit' ) {
            $('#venetie').show(); 
        } else {
            $('#venetie').hide(); 
        } 
    });

因此,如果您选择20 Fruit,那么div #venetie将显示。 但我希望如果你也选择30辆汽车,那么div #venetie也将展示。

我试过这个:

 $('#CoachTravel').change(function(){
        if($('#CoachTravel').val() == '20 Fruit', '30 Cars' ) {
            $('#venetie').show(); 
        } else {
            $('#venetie').hide(); 
        } 
    });

但现在这对我有用,有人可以帮帮我吗? 这是一个Jsfiddle:

https://jsfiddle.net/1Lfgrwjj/5/

5 个答案:

答案 0 :(得分:0)

如果条件允许,您可以使用或运算符:

var coachTravel = $('#CoachTravel').val();
if( coachTravel == '20 Fruit' ||  coachTravel =='30 Cars' ) {

您也可以使用切换功能中的条件缩小代码范围,以显示隐藏决策:

$('#CoachTravel').change(function(){
   var coachTravel = $('#CoachTravel').val();
   $('#venetie').toggle(coachTravel == '20 Fruit' ||  coachTravel =='30 Cars'); 
});

<强> Working Demo

答案 1 :(得分:0)

您可以像这样使用逻辑或static void TestAttempt() { string source = @" Imports System Namespace Exceptions Public NotInheritable Class ExampleException Inherits Validation Public Sub New() Convert.ToString(""Ignore me 1"") Console.WriteLine(""Report me"") Console.WriteLine(Convert.ToString(""Ignore me 2"")) MyBase.New(Convert.ToString(""Ignore me 3, "" & _ ""Because I'm already translated."")) End Sub End Class End Namespace"; var tree = VisualBasicSyntaxTree.ParseText(source); var syntaxRoot = tree.GetRoot(); int i = 0; foreach (var s in syntaxRoot.DescendantNodes().OfType<LiteralExpressionSyntax>()) { // things to skip: if (s == null) { continue; } if (s.Kind() != SyntaxKind.StringLiteralExpression) { continue; } var Mscorlib = PortableExecutableReference.CreateFromFile(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll"); var compilation = VisualBasicCompilation.Create("MyCompilation", syntaxTrees: new[] { tree }, references: new[] { Mscorlib }); var model = compilation.GetSemanticModel(tree); var symbol = SymbolFinder.FindSymbolAtPositionAsync(model, s.Parent.Parent.Parent.Span.Start, new AdhocWorkspace()).Result; if (symbol.ToDisplayString().EndsWith("Convert")) { continue; } Console.WriteLine(symbol); Console.WriteLine($" Reported: {s.ToString()}"); i++; } Console.WriteLine(); Console.WriteLine($"Total: {i}"); }

||

答案 2 :(得分:0)

 $('#CoachTravel').change(function(){
      if($('#CoachTravel').val() == '20 Fruit' || $('#CoachTravel').val() == '20 Fruit' ) {
         $('#venetie').show(); 
       } else {
          $('#venetie').hide(); 
      } 
   });

答案 3 :(得分:0)

您可以将if条件更改为:

if( $.inArray($('#CoachTravel').val(), ['20 Fruit', '30 Cars']) > -1)

使用函数$('#CoachTravel')检查['20 Fruit', '30 Cars']的值是否在数组inArray中。

答案 4 :(得分:0)

&#13;
&#13;
$('#venetie').hide(); 
$('#CoachTravel').change(function(){
  var Select_value = $('#CoachTravel').val();
  if(Select_value == '20 Fruit' ||  Select_value == '30 Cars') {
      $('#venetie').show(); 
  } else {
      $('#venetie').hide(); 
} 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="bestemming" id="CoachTravel" class="form-control select" >
<option value="10 Glasses" >10 Glasses</option>
<option value="20 Fruit" >20 Fruit</option>
<option value="30 Cars" >30 Cars</option>
</select>
<div class="form-group" id="venetie" >
<input type="radio" name="radio" id="radio" value="radio">
<label for="radio">Ja </label>
<input type="radio" name="radio" id="radio" value="radio">Nee</div>
&#13;
&#13;
&#13;

请检查代码使用 OR 运算符,您可以获得所需的结果。

谢谢