2011-06-26 213 views
0

這裏是用於選擇單選按鈕的jquery腳本。選擇單選按鈕

function radio(){ 
     var presetValue = "scholar"; 
$("[name=identity]").filter("[value="+presetValue+"]").attr("checked","checked"); 
} 

腳本在頁面加載時被調用。 這是代碼XHTML和平:

<label for="scholar"> Scholar </label> 
     <input type="radio" name="identity" id="scholar" value="scholar" /> 
     </td> 

     <td> 
     <label for="oracle"> Oracle </label> 
     <input type="radio" name="identity" id="oracle" value="oracle"/> 
     </td> 

     <td> 
     <label for="both"> Both </label> 
     <input type="radio" name="identity" id="both" value="both"/> 

當程序運行時那學者「單選按鈕沒有被選中。 可能是什麼問題? 最好的問候

回答

1

如果你正在使用jQuery> = 1.6,你應該使用prop()

此外,你正在使用一個複雜的選擇器爲空。由於您的輸入有ID的,這將做到這一點:

$('#scholar').prop('checked', true); 

如果presetValue變化(即:簡化你的代碼),這樣做:

$('#' + presetValue).prop('checked', true); 
0
function radio(){ 
     var presetValue = "scholar"; 
$("input[value="+presetValue+"]").attr("checked",true); 
}