我有以下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'文本框,然后将所有选择的值(从这些RadioButtonList中的大约10个)计算到'txtSomeTotal'文本框中。
我对jQuery很陌生。任何帮助都是极好的。
谢谢
最佳答案
您的主要问题是选择单选按钮,因为radiobuttonlist id与包含您的单选按钮的表有关。一旦知道了,这只是获取要更新的字段的一种情况。我建议为
<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>
09-25 17:39