如果用户从单选按钮中选择“是”,我试图显示警告消息。我的问题是警告消息仅显示“是”,但我想显示“是”和“否”,因此用户可以选择其中一个。当他们选择“是”时,我将删除一些记录,但是所有操作都在后面的代码中完成。我在这里做错了什么?

<div>

    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
        OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
        <asp:ListItem Value="0">Yes</asp:ListItem>
        <asp:ListItem Value="1" Selected="True">No</asp:ListItem>

    </asp:RadioButtonList>

</div>


这是JavaScript

<script type="text/javascript">

    var selectlistId =   document.getElementsByName('<%= RadioButtonList1.ClientID %>');
    selectlist = document.getElementByid(selectlistId);

    selectlist.onchange = function () {
        if (selectlist.options[selectlist.selectedIndex].value == "YES") {
            if (confirm("Are you sure you want to delete?")) {
                __doPostBack(selectlistId, '');
            } else {
                // User selected NO, so change DropDownList back to 0.
                selectlist.selectedIndex = 0;
            }
        }
    };
</script>

最佳答案

回传使用.name,而不是id,请尝试更改为

__doPostBack(selectlist.name, '');


看看是否有帮助

至于警告消息,您必须将事件处理程序附加到单选按钮列表内的输入元素。单选按钮列表本身是一个选项表。所以从我的头顶上,这样的事情应该工作

$('#<%=RadioButtonList1.ClientID%> input').click(function(){
    var $rb = $(this);
    if ($rb.val() == "YES"){
        ...
    }
});


编辑:
我添加了一个代码示例,演示了未使用jquery的工作确认框:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
                     OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
    <asp:listitem value="0">Yes</asp:listitem>
    <asp:listitem value="1" selected="True">No</asp:listitem>

</asp:RadioButtonList>


<script>

    (function () {

        //without jquery
        var rbl = document.getElementById('<%=RadioButtonList1.ClientID%>');

        var chks = rbl.getElementsByTagName('input');

        for (var x = 0; x < chks.length; x++) {
            var chk = chks[x];

            chk.onclick = function () {
                var that = this;
                if (that.value == "0") {

                    //selected yes
                    if (confirm("Are you sure?")) {

                        //confirmed yes
                        __doPostBack(rbl.name, '');
                    }
                    else {
                        //confirmed no
                    }
                }
                else {
                    //selected no
                }
            }
        }



    })();

</script>

09-12 07:37