【问题标题】:Validate RadioButtonList value with jQuery before submit在提交之前使用 jQuery 验证 RadioButtonList 值
【发布时间】:2015-10-22 07:49:43
【问题描述】:

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

【问题讨论】:

    标签: c# jquery asp.net webforms


    【解决方案1】:

    给你。这就是您应该如何确定检查项目的价值。将“test”替换为您在查看页面源代码时看到的两个列表项中的名称元素。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2023-03-02
      相关资源
      最近更新 更多