简体   繁体   English

使用JavaScript显示警告消息

[英]Use JavaScript to show warning message

I am trying to show a warning message if the users select Yes from the radio button. 如果用户从单选按钮中选择“是”,我试图显示警告消息。 My issue is that the warning message is only showing Yes but i would like to show Yes and No so the user can select either of them. 我的问题是警告消息仅显示“是”,但我想显示“是”和“否”,因此用户可以选择其中一个。 When they select Yes then i am deleting some records but all that is being done in the code behind. 当他们选择“是”时,我将删除一些记录,但是所有操作都在后面的代码中完成。 What am i doing wrong here? 我在这里做错了什么?

<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>

here is the javascript 这是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>

postbacks work with .name, not id, try changing to 回传使用.name,而不是id,请尝试更改为

__doPostBack(selectlist.name, '');

and see if that helps 看看是否有帮助

As for the warning message, you have to attach the event handler to the input elements inside the radio button list. 至于警告消息,您必须将事件处理程序附加到单选按钮列表内的输入元素。 The radio button list itself is a table of options. 单选按钮列表本身是一个选项表。 So off the top of my head, something like this should work 所以从我的头顶上,这样的事情应该工作

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

EDIT: I added a code sample demonstrating a working confirm box NOT using jquery: 编辑:我添加了一个代码示例,演示了不使用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>
  <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" RepeatDirection="Horizontal" ClientID="static"
    OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
       <asp:ListItem Value="0">Yes</asp:ListItem>
       <asp:ListItem Value="1" Selected="True">No</asp:ListItem>

  </asp:RadioButtonList>

JS JS

$("#RadioButtonList1").change(function(){

   /// Do what you want

 });

Explanation : You might want to add static ID(ClientID="static") to the asp.net control and then attach an event to that ID. 说明 :您可能想将静态ID(ClientID =“ static”)添加到asp.net控件,然后将事件附加到该ID。

The confirm dialog only shows yes or cancel. 确认对话框仅显示是或取消。 How about using a jquery dialog? 如何使用jQuery对话框? This will allow you to change the text in the dialog box for your different options. 这将允许您更改对话框中用于不同选项的文本。 API documentation is here . API文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM