检查是否已选择radiobuttonlist

时间:2014-01-16 11:53:51

标签: javascript asp.net

我正在使用此代码检查是否已选择radiobuttonlist,但如果未检查任何内容,则不会返回false,并继续执行后面的类中的函数。 radiobutton列表是从代码隐藏中的数据绑定生成的。

<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="checkform()">Delete Template</asp:LinkButton>

<asp:radiobuttonlist id="optMessageType" runat="server" onselectedindexchanged="optMessageType_SelectedIndexChanged" RepeatDirection="Vertical" CssClass="none-table">
</asp:radiobuttonlist>

function checkform() {
                var radiolist = document.getElementById('<%= optMessageType.ClientID %>');
                var radio = radiolist.getElementsByTagName("input");
                if (radio.length > 0) {
                    for (var x = 0; x < radio.length; x++) {
                        if (radio[x].type === "radio" && radio[x].checked) {
                            //alert("Checking...");
                            // alert("Selected item Value " + radio[x].value);
                            var r = confirm("Are you sure you want to delete this template?")
                            if (r == true) {
                                //alert("You pressed OK!")
                                return true;
                            }
                            else {
                                //alert("You pressed Cancel!");
                                return;
                            }
                        }
                        else {
                            alert("Select a template to delete");
                            return;
                        }
                    }
                }
                else {
                    alert("nope");
                    return;
                }
            }

代码背后:

protected void link_Delete(object sender, System.EventArgs e)
{
Label3.Text = "Deleted!"; //For Testing
}

1 个答案:

答案 0 :(得分:0)

你应该写

<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="return checkform();">Delete Template</asp:LinkButton>

我在return属性函数调用中添加了OnClientClick 希望这可以解决我们的问题。