我有以下HTML代码:
<tr>
<td>
<span>Random question here?</span>
</td>
<td>
<asp:RadioButtonList ID="someList" runat="server" SelectedValue="<%# Bind('someValue') %>" RepeatDirection="Horizontal" CssClass="radioList">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="4"></asp:ListItem>
<asp:ListItem Text="Don't Know" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtSomeValue" runat="server" Height="16px" CssClass="someScore" Enabled="false" Text="0"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtSomeTotal" runat="server" Height="16px" CssClass="someTotal" Enabled="false" Text="0"></asp:TextBox>
<asp:Label ID="lblTFTotal" runat="server" Font-Bold="true" Font-Italic="true" Text="Your selected value is"></asp:Label>
</td>
</tr>
我需要编写一个jQuery函数,用从RadioButtonList中选择的值填充'txtSomeValue'TextBox,然后计算所选的所有值(从这些RadioButtonLists中的大约10个)到'txtSomeTotal'TextBox。
我对jQuery很新。任何帮助都会很棒。
由于
答案 0 :(得分:6)
伊万几乎得到了它 - 但是有更快的方法。使用JQuery选择RBL,然后按所选输入进行过滤:
$("#<%# rbRadioButtonListId.ClientID %> input:radio:checked").val();
无需捕获表格或单选按钮的名称。
答案 1 :(得分:3)
列表中的所有单选按钮都应使用相同的名称属性进行渲染。看一下,并将其用作脚本中的选择器。这样的事情应该有效:
$('input:radio[name=NameOfRadioButtons]:checked').val();
答案 2 :(得分:2)
您的主要问题是选择单选按钮,因为radiobuttonlist id与包装单选按钮的表格相关。一旦你有了它,只需要获取你想要更新的字段。我建议将一个类分配给&lt; tr&gt;标记使这更容易:
<tr class="mainRow">
<td>
<span>Random question here?</span>
</td>
<td> ....
然后使用类似的东西
<script>
$(document).ready(function () {
$('.radioList input').click(function () {
/* get the value of the radio button that's been clicked */
var selectedValue = $(this).val();
/* assign it to the nearest text box with a class of someScore */
$(this).closest('.mainRow').find('.someScore').val(selectedValue);
/* calculate the value of all the text boxes with a class of someScore */
var currentTotal = 0;
$('.someScore').each(function () {
if (!isNaN($(this).val())) {
currentTotal += Number($(this).val());
}
});
/* assign it to the text box that displays the total */
$('#<%=txtSomeTotal.ClientID %>').val(currentTotal);
});
});
</script>
答案 3 :(得分:1)
要使用jQuery填充文本框,您应该使用val() - 函数。
$("#ID").val("Text to put in the textbox").
因为ASP.NET更改了.net对象的ID,所以你必须这样做:
$("#<%= txtSomeValue.ClientID %>").val("Text to put in the textbox").
这还够吗?
答案 4 :(得分:0)
我用过:
$('#<%= rbRadioButtonListId.ClientID %>').children().children().children().find("input:checked").val();
“儿童”选择器太多,因为生成的html代表radiobuttonlist(表&gt; tbody&gt; tr&gt; td&gt;输入)。
希望这个答案可以帮助其他人找不到合适的解决方案。
答案 5 :(得分:0)
<asp:RadioButtonList ID="rblRequestType">
<asp:ListItem Selected="True" Value="value1">Value1</asp:ListItem>
<asp:ListItem Value="Value2">Value2</asp:ListItem>
</asp:RadioButtonList>
您可以访问此类列表项;
var radio0 = $("#rblRequestType_0");
var radio1 = $("#rblRequestType_1");
if (radio0.checked){
// do something
}
if (radio1.checked){
// do something
}