44

我有类似于以下的代码...

<p><label>Do you have buffet facilities?</label>
  <asp:RadioButtonList ID="blnBuffetMealFacilities:chk" runat="server">
    <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="0"></asp:ListItem>
  </asp:RadioButtonList></p>
<div id="HasBuffet">
  <p><label>What is the capacity for the buffet?</label>
  <asp:RadioButtonList ID="radBuffetCapacity" runat="server">
    <asp:ListItem Text="Suitable for upto 30 guests" value="0 to 30"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 50 guests" value="30 to 50"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 75 guests" value="50 to 75"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 100 guests" value="75 to 100"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 150 guests" value="100 to 150"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 250 guests" value="150 to 250"></asp:ListItem>
    <asp:ListItem Text="Suitable for upto 400 guests" value="250 to 400"></asp:ListItem>
  </asp:RadioButtonList></p>
</div>

我想在收音机列表 blBuffetMealFacilities:chk 更改客户端并在 jQuery 的 HasBuffet div 上执行向下滑动功能时捕获一个事件。创建这个的最佳方法是什么,请记住页面上有几个类似的部分,我希望根据广播列表中的“是”或“否”答案来揭示问题。

4

13 回答 13

87

检索 RadioButtonList1 的检查值的简单方法是:

$('#RadioButtonList1 input:checked').val()

蒂姆编辑:

其中RadioButtonList1必须是 RadioButtonList 的 ClientID

var rblSelectedValue = $("#<%= RadioButtonList1.ClientID %> input:checked"); 
于 2010-05-21T03:59:54.347 回答
29

这:

$('#rblDiv input').click(function(){
    alert($('#rblDiv input').index(this));
});

将为您提供单击的单选按钮的索引(我认为,未经测试)(请注意,您必须将 RBL 包装在 #rblDiv 中

然后,您可以使用它来显示相应的 div,如下所示:

$('.divCollection div:eq(' + $('#rblDiv input').index(this) +')').show();

这是你的意思吗?

编辑:另一种方法是给 rbl 一个类名,然后去:

$('.rblClass').val();
于 2008-11-21T10:07:37.083 回答
14

这对我有用...

<asp:RadioButtonList runat="server" ID="Intent">
  <asp:ListItem Value="Confirm">Yes!</asp:ListItem>
  <asp:ListItem Value="Postpone">Not now</asp:ListItem>
  <asp:ListItem Value="Decline">Never!</asp:ListItem>
</asp:RadioButtonList>

处理程序:

$(document).ready(function () {
  $("#<%=Intent.ClientID%>").change(function(){
   var rbvalue = $("input[name='<%=Intent.UniqueID%>']:radio:checked").val();

   if(rbvalue == "Confirm"){
    alert("Woohoo, Thanks!");
   } else if (rbvalue == "Postpone"){
    alert("Well, I really hope it's soon");
   } else if (rbvalue == "Decline"){
    alert("Shucks!");
   } else {
    alert("How'd you get here? Who sent you?");
   }
  });
});

重要部分: $("input[name='<%=Intent.UniqueID%>']:radio:checked").val();

于 2011-10-05T19:58:31.213 回答
6

据我说,它会工作得很好......

试试这个

   var GetValue=$('#radiobuttonListId').find(":checked").val();

要存储在 GetValue(Variable) 上的 Radiobuttonlist 值。

于 2012-05-03T06:42:49.207 回答
4

单选按钮列表而不是单选按钮会创建唯一的 id 标签 name_0、name_1 等。测试哪个被选中的简单方法是分配一个 CSS 类,例如

var deliveryService;
$('.deliveryservice input').each(function () {
    if (this.checked) {
        deliveryService = this.value
    }
于 2011-06-10T07:23:07.687 回答
4

试试这个:

$("input[name='<%=RadioButtonList1.UniqueID %>']:checked").val()
于 2014-10-02T13:04:56.793 回答
1

以下是我们最终创建的代码。先简单解释一下。我们使用“q_”作为环绕单选按钮问题列表的 div 名称。然后我们对任何部分都有“s_”。以下代码循环遍历问题以找到选中的值,然后在相关部分执行滑动操作。

var shows_6 = function() {
  var selected = $("#q_7 input:radio:checked").val();
  if (selected == 'Groom') {
    $("#s_6").slideDown();
  } else {
    $("#s_6").slideUp();
  }
};
$('#q_7 input').ready(shows_6);
var shows_7 = function() {
  var selected = $("#q_7 input:radio:checked").val();
  if (selected == 'Bride') {
    $("#s_7").slideDown();
  } else {
    $("#s_7").slideUp();
  }
};
$('#q_7 input').ready(shows_7);
$(document).ready(function() {
  $('#q_7 input:radio').click(shows_6);
  $('#q_7 input:radio').click(shows_7);
});

<div id="q_7" class='question '><label>Who are you?</label> 
  <p>
    <label for="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_0">Bride</label>
    <input id="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_0" type="radio" name="ctl00$ctl00$ContentMainPane$Body$ctl00$ctl00$chk" value="Bride" />
  </p> 
  <p>
    <label for="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_1">Groom</label>
    <input id="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_1" type="radio" name="ctl00$ctl00$ContentMainPane$Body$ctl00$ctl00$chk" value="Groom" />
  </p> 

</div> 

以下允许我们将问题设为强制性...

<script type="text/javascript"> 
var mandatory_q_7 = function() {
  var selected = $("#q_7 input:radio:checked").val();
  if (selected != '') {
    $("#q_7").removeClass('error');
  }
};
$(document).ready(function() {
    $('#q_7 input:radio').click(function(){mandatory_q_7();});
});
</script> 

这是实际显示/隐藏层的示例

<div class="section" id="s_6"> 
    <h2>Attire</h2> 
    ...
</div>
于 2009-04-02T08:22:24.580 回答
1

试试下面的代码:

<script type="text/javascript">
    $(document).ready(function () {

        $(".ratingButtons").buttonset();

    });
 </script>


<asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server"
            AutoPostBack="True" DataSourceID="SqlDataSourceSizes"     DataTextField="ProdSize"
            CssClass="ratingButtons" DataValueField="_ProdSizeID" Font-Size="X-Small" 
            ForeColor="#666666">
</asp:RadioButtonList>
于 2012-07-03T17:13:36.480 回答
1

为什么这么复杂?

$('#id:checked').val();

会工作得很好!

于 2014-01-27T12:35:51.860 回答
0

我找到了一个简单的解决方案,试试这个:

var Ocasiao = "";    
$('#ctl00_rdlOcasioesMarcas input').each(function() { if (this.checked) { Ocasiao = this.value } });
于 2009-05-20T11:22:59.160 回答
0

Andrew Bullock 解决方案工作得很好,我只是想向您展示我的解决方案并添加警告。

//效果很好

$('#<%= radBuffetCapacity.ClientID %> input').click(function (e) {
   var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
   //Do whatever
});

//警告 - 适用于 Firefox 但不适用于 IE8 .. 在注意到它在 IE8 中不起作用之前使用了一段时间......当在一个浏览器中使用 jQuery 时,它习惯于在所有浏览器中工作。

$('#<%= radBuffetCapacity.ClientID %>').change(function (e) {
   var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
   //Do whatever
});
于 2010-09-17T10:14:26.957 回答
0

我投票支持 Vinh 的答案以获得价值。

如果需要找到对应的标签,可以使用这段代码:

$('#ClientID' + ' input:checked').parent().find('label').text()

于 2011-07-15T14:44:30.807 回答
0

对于我的场景,我的 JS 位于一个单独的文件中,因此使用 ClientID 响应输出是不利的。令人惊讶的是,解决方案就像在 RadioButtonList 中添加一个 CssClass 一样简单,这是我在DevCurry上发现的

以防万一该解决方案消失,添加一个类到您的单选按钮列表

<asp:RadioButtonList id="rbl" runat="server" class="tbl">...

正如文章所指出的,当单选按钮列表被渲染时,类“tbl”被附加到周围的表中

<table id="rbl" class="tbl" border="0">
<tr>...

现在由于附加了 CSS 类,您可以根据 css 类选择器在表中引用 input:radio 项目

$(function () {
            var $radBtn = $("table.tbl input:radio");
            $radBtn.click(function () {
                var $radChecked = $(':radio:checked');
                alert($radChecked.val());
            });
        });

同样,这避免了使用上面提到的“ClientID”,我发现我的场景很混乱。希望这可以帮助!

于 2014-01-21T17:16:35.053 回答