2014-12-04 43 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