在jQuery中嵌套If语句

时间:2014-12-04 18:07:06

标签: jquery if-statement mobile

我正在尝试在if语句中嵌入一个单选按钮if if,我不知道我把它放在哪里。

我希望该功能可以“如果国家/地区的价值是英国,则选中单选按钮”。

代码如下。

$('#submit').click (function(){    

      $.mobile.navigate("#results-page")

      if ($("#country").val()==("UK"))  
      { 
           $( "#important1" ).text(UK.item1 ); 
           $( "#important2" ).text(UK.item2 );
           $( "#important3" ).text(UK.item3 );
      }

      if ($("#country").val()==("USA"))  
      { 
           $( "#important1" ).text(USA.item1 ); 
           $( "#important2" ).text(USA.item2 );
           $( "#important3" ).text(USA.item3 ); 
      }

      if ($("#country").val()==("France")) 
      { 
           $( "#important1" ).text(France.item1 ); 
           $( "#important2" ).text(France.item2 );
           $( "#important3" ).text(France.item3 );   
      }
  }) 

HTML(国家/地区)

<select name="country" id="country">
      <option value="UK">UK</option>
      <option value="USA">USA</option>
      <option value="France">France</option>
</select>

HTML(假日类型)              野营

  <input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2"  />
  <label for="radio-choice-2">Clubbing</label>

  <input type="radio" name="radio-choice" id="radio-choice-3" value="choice-3"  />
  <label for="radio-choice-3">Luxury</label>

三江源

4 个答案:

答案 0 :(得分:0)

不是嵌套另一个if语句,而是将条件添加到现有的if语句

//assuming radioButton is the id
if(($("#country").val()==("UK")) && ($("radioButton").val() == true))  

答案 1 :(得分:0)

你可以用两种方式做到: 1.将它放在如下所示的if语句中:

    if ( $("#country").val()==("UK") && $("#radiobutton").is(":checked")  {
        //do something
    }
  1. 或者您可以将其嵌套在if语句中,如下所示:

    if ( $("#country").val()==("UK"))  {
        //do something
        if ($("#radiobutton").is(":checked") {
            //do something because the radio button conditional is satisfied
        }
    }
    

答案 2 :(得分:0)

您可以使用以下内容查看是否已选中:

   $('#radio_button').is(':checked')

你的代码最终会像:

$('#submit').click (function(){    
  $.mobile.navigate("#results-page")

  if ($("#country").val()==("UK") && $('#radio_button').is(':checked')))
   { 
       $( "#important1" ).text(UK.item1 ); 
       $( "#important2" ).text(UK.item2 );
       $( "#important3" ).text(UK.item3 );
   }

   if ($("#country").val()==("USA") && $('#radio_button').is(':checked')))
   { 
       $( "#important1" ).text(USA.item1 ); 
       $( "#important2" ).text(USA.item2 );
       $( "#important3" ).text(USA.item3 ); 
   }

    if ($("#country").val()==("France") && $('#radio_button').is(':checked')))
   { 
       $( "#important1" ).text(France.item1 ); 
       $( "#important2" ).text(France.item2 );
       $( "#important3" ).text(France.item3 );   
   }
 });

答案 3 :(得分:0)

var conutry = $("#country").val();

if ( country == "UK" && $("#radiobutton").is(":checked") )  {
    //do something
}