在提交之前用jQuery验证RadioButtonList的值
本文关键字:验证 RadioButtonList 的值 jQuery 提交 | 更新日期: 2023-09-27 18:05:32
我不擅长jquery,我试图阻止。net表单提交,如果用户选择是(1)在RadioButtonList路径。下面是我的代码,我不知道为什么它不起作用。
当我运行。net应用程序时,表单名称变为aspnetform, radPathway RadioButtonList变为#ctl00$ContentPlaceHolder1$radPathway:
<script>
$('#aspnetForm').submit(function () {
if ($('#ctl00$ContentPlaceHolder1$radPathway').val() === "1") {
alert('Stop!');
return false;
}
});
</script>
。aspx代码:
<p>
<asp:Label ID="Label23" runat="server" Font-Bold="True"
Text="Pathway member?"></asp:Label>
<asp:RadioButtonList ID="radPathway" runat="server" RepeatDirection="Horizontal" Width="50px">
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server"
ControlToValidate="radPathway" ErrorMessage="Please select your pathway status" ForeColor="red">
</asp:RequiredFieldValidator>
<asp:Label ID="lblpathway" runat="server" ForeColor="Red" Text="Label" Visible="False"></asp:Label>
</p>
给你。这就是您应该如何确定选中项的值。当您查看页面的源代码时,将两个列表项中的"test"替换为name元素。
function getCheckedRadio() {
var radioButtons = document.getElementsByName("test");
for (var x = 0; x < radioButtons.length; x ++) {
if (radioButtons[x].checked) {
var selectedValue = radioButtons[x].value;
}
}
}
if (selectedValue == "1") {
//rockmysocks
}