2011-02-04 120 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()