2011-02-04 122 views
1

我有一個單選按鈕組(請參見下面的代碼):單選按鈕組

<tr> 
       <td> 
        Total Meter : 
       </td> 
       <td style="width:10px;"></td> 
       <td> 
        <input type="radio" name="rdoInput" value="Yes" style="border-style:none;" /> Yes 
       </td> 
       <td style="width:10px;"></td> 
       <td>  
        <input type="radio" name="rdoInput" value="No" style="border-style:none;" /> No 
       </td> 
      </tr> 

我的問題是,我該如何設置和獲取這些資產的價值?我試過這個語法來設置值,但它不起作用:

if (msg.d.Input == true) { 
         $('input[name="rdoInput"]').attr('checked', true); 
        } 
        else { 
         $('input[name="rdoInput"]').attr('checked', false); 
        } 

任何幫助將不勝感激!

+3

該表的佈局看起來令人驚訝......尤其是那些空單元格仿效填充左側。 `:D` – 2011-02-04 16:03:23

+0

它不起作用?你的意思是像一個錯誤被拋出? – 2011-02-04 16:05:51

回答

2
$("input[name='rdoInput'][value='Yes']").attr("checked", true); 

$("input[name='rdoInput']:eq(0)").attr("checked", true); 

更好的方式:

var value = sg.d.Input ? "Yes" : "No"; 

// set the value 
$("input[name='rdoInput'][value='" + value + "']").attr("checked", true); 

// get the value 
var checkedValue = $("input[name='rdoInput']:checked").val(); 

工作示例:http://jsfiddle.net/xVDxZ/1/

1

運用EQ(N):

$('input[name="rdoInput"]').eq(0).attr('checked', true); 
0

爲了獲得該組的當前簽單選按鈕的值,使用:

$('input:radio[name="rdoInput"]:checked').val()