在提交之前使用jQuery验证RadioButtonList值

时间:2015-07-30 19:31:50

标签: c# jquery asp.net webforms

我不擅长jquery,我试图阻止.net表单提交,如果用户在RadioButtonList路径中选择Yes(1)。下面是我的代码,我不知道它为什么不起作用 运行.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>
    &nbsp;
    <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>

1 个答案:

答案 0 :(得分:0)

你走了。这是您应该如何确定已检查项目的值。替换&#34;测试&#34;在您查看页面来源时看到的两个列表项上都有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
}