-1

我有单选按钮列表和一个带有以下代码的文本框:

   <asp:RadioButtonList ID="RadioButtonList" runat="server" RepeatDirection="Horizontal">
   <asp:ListItem Text="Enabled" Value="1"></asp:ListItem>
   <asp:ListItem Text="Disabled" Value="2"></asp:ListItem>
   </asp:RadioButtonList>

   <asp:TextBox ID="enable" runat="server" value="Enabled"></asp:TextBox>

现在我希望选择/检查单选按钮列表中的第一个列表,但 mu jquery 代码不起作用。任何人都可以帮助我吗?我写我的jQuery代码如下:

    if($('#<%= enable.ClientID %>').val()=="Enabled")
        $('#<%= RadioButtonList.ClientID %>').val(1);
   else($('#<%= enable.ClientID %>').val()=="Disabled")
        $('#<%= RadioButtonList.ClientID %>').val(2);
4

4 回答 4

2

试试这个

if($('#<%= enable.ClientID %>').val()=="Enable")
        $('#<%= roleDropDownList.ClientID %> input[value="1"]').attr('checked','checked');
于 2012-09-28T06:43:00.953 回答
1

.val 是一种方法,而不是一种属性..

正确的语法是.val()

看起来您的 RadioButtonList id 也是RadioButtonList ..

但是您正在尝试选择 $('#<%= roleDropDownList.ClientID %>')

   if($('[id*=enable]').val() == "Enabled"){
        $('[id*=RadioButtonList] :radio[value="1"] ').prop('checked', true);
} 

更新代码

$(function() {
    $('#<%= enable.ClientID %>').on('change', function() {
        var val = $(' #<%=  enable.ClientID %> ').val();
        if (val == "Enabled") {
          $('#<%=RadioButtonList.ClientID%> :radio[value="1"]').prop('checked', true);
        }
        else if( val == "Dnabled" ) {
          $('#<%=RadioButtonList.ClientID%> :radio[value="2"]').prop('checked', true);
        }
    }).change();
});

检查演示

于 2012-09-28T06:46:29.877 回答
0

试试这个

  var set_value=2;
  $('#RadioButtonList').filter("[value='"+set_value+"']").attr("checked","checked");
于 2012-09-28T06:44:28.403 回答
0

试试这个方法

$('#<%= enable.ClientID %> input').click(function() {
    alert('Selected Value: '+$("#<%= enable.ClientID %> input:radio:checked").val());
});
于 2012-09-28T06:45:21.467 回答