簡體   English   中英

使用JavaScript顯示警告消息

[英]Use JavaScript to show warning message

如果用戶從單選按鈕中選擇“是”,我試圖顯示警告消息。 我的問題是警告消息僅顯示“是”,但我想顯示“是”和“否”,因此用戶可以選擇其中一個。 當他們選擇“是”時,我將刪除一些記錄,但是所有操作都在后面的代碼中完成。 我在這里做錯了什么?

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

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

   /// Do what you want

 });

說明 :您可能想將靜態ID(ClientID =“ static”)添加到asp.net控件,然后將事件附加到該ID。

確認對話框僅顯示是或取消。 如何使用jQuery對話框? 這將允許您更改對話框中用於不同選項的文本。 API文檔在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM