2014-12-04 117 views
-2

我试图嵌套一个单选按钮,如果在if语句中的语句,我不知道我把它放在哪里。嵌套if语句在jQuery中

我想功能做“如果国家的价值是英国和单选按钮被选中的话..”

的代码如下所示。

$('#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> 

三江源

+0

哪个单选按钮???然后什么?您应该提供相关的HTML标记 – 2014-12-04 18:08:58

+0

您应该也可以发布标记以提供一些上下文 – andrew 2014-12-04 18:10:28

+0

您可以显示HTML吗? '#country'是你的单选按钮吗? – putvande 2014-12-04 18:10:29

回答

0

相反的if语句嵌套另一个,加入的条件现有的if语句

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

你可以做两种方式:1。 把它在同一个if语句象下面这样:

if ($("#country").val()==("UK") && $("#radiobutton").is(":checked") { 
     //do something 
    } 
  • ,或者你可以嵌套它在if语句象下面这样:

    if ($("#country").val()==("UK")) { 
        //do something 
        if ($("#radiobutton").is(":checked") { 
         //do something because the radio button conditional is satisfied 
        } 
    } 
    
  • 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); 
        } 
    }); 
    
    0
    var conutry = $("#country").val(); 
    
    if (country == "UK" && $("#radiobutton").is(":checked")) { 
        //do something 
    } 
    
    +0

    谢谢,它工作得很好! – 2014-12-04 19:03:50