2010-04-14 43 views
2

我試圖在用戶選擇單選按鈕組內的特定單選按鈕(Other)時使用jQuery顯示div。jQuery:在單選按鈕上選擇顯示div

的HTML低於

<div id="countries"> 

<input id="Washington_D.C" type="radio" name="location" value="Washington">Washington D.C</input> 
<input id="Other" type="radio" name="location" value="">Other</input> 
    <div id="other locations" style="display: none"> 
    </div> 
</div> 

使用jQuery代碼:

$(document).ready(function(){ 
    $("radio[@name='location']").change(function(){ 
     if ($("radio[@name='location']:checked").val() == 'Other') 
     $("#county_drop_down").show(); 
    }); 
}); 

但它沒有顯示在div其他位置時,我選擇單選按鈕Other

回答

7

您需要給它一個值Other

因此,沒有那麼

<input id="Other" TYPE="RADIO" NAME="location" VALUE="">Other</input> 

但如此

<input id="Other" TYPE="RADIO" NAME="location" VALUE="Other">Other</input> 

乍一看,你的jQuery代碼看起來不錯。不過,如果沒有預選方式,我會傾向於以上change,因爲change在第一次點擊時不會被解僱。

2

嘗試

$("radio[name='location']").change(function(){ 
    if ($(this).val() === 'Other') 
    { 
     if($(this).is(":checked")) 
     { 
      $("#county_drop_down").show(); 
     } 
    } 
}); 

的單選按鈕值設置爲其他此代碼工作。

相關問題