簡體   English   中英

在javascript中詢問是/否問題的代碼

[英]Code to ask yes/no question in javascript

我只能找到提供確定/取消按鈕的功能confirm() 有沒有辦法給是/否按鈕?

Javascript 提供了 3 個模態框。 promptconfirmalert 這些都不能滿足您的要求。

有大量的 js 模態彈出解決方案。 這是一個例子。

沒有。

相反,您可以使用瀏覽器模式彈出窗口。

就像上面的其他人說的一樣,您使用confirm()被困在 OK/Cancel 上。

我想推薦這個jQuery插件: jqModal 我最近在 3 個項目中使用了它,並且對每個項目都非常有效。 具體看看這個例子:

6). 好玩! 覆蓋 - a. 查看(警報),b。 view (confirm) 現在是展示 jqModal 的實際用途的時候了——覆蓋標准的 alert() 並確認對話框! 注意; 由於 javascript 的單線程特性,confirm() 函數必須傳遞一個回調——它不返回真/假。

不,但有一些 JavaScript 庫可以為您完成此任務。 例如, Ext JS可用於創建 消息框對話框

對於這類事情,我是jQuery UI Dialog的粉絲。 這是一個示例...

<script>
  $(function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:140,
      modal: true,
      buttons: {
        "Yes": function() {
          $( this ).dialog( "close" );
          alert("You chose Yes!");
        },
        "No": function() {
          $( this ).dialog( "close" );
          alert("You chose No!");
        }
      }
    });
  });
  </script>

<div id="dialog-confirm" title="Are you sure you want to continue?">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

我會使用sweetalert https://sweetalert.js.org/guides/來實現這樣的事情

 swal("Are you sure you want to do this?", { buttons: ["yes", "no"], });
 <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

使用對話框顯示是或否

           <div id="dialog_box" class="mnk-modal-bg" style="display:none">
              <div id="dbg" class="mnk-modal-box">
                <i class="uk-icon-exclamation-triangle"  style="color:#757575; padding-right:5px;">
                </i>Confirm?
                <div class="uk-text-center" style="margin-top:10px;">
                    <button class="md-btn md-btn-small md-btn-primary" id="ok_btn">
                        <i class="uk-icon-save" style="padding-right:3px;"></i>OK
                    </button>
                    <button class="md-btn md-btn-small md-btn-danger" id="close_btn">
                        <i class="uk-icon-remove" style="padding-right:3px;"></i>Cancel
                    </button>
                </div>
            </div>

<script>
    $("#ok_btn").click(function(){
        alert("OK");
        $("#dialog_box").hide();
    });
    $("#close_btn").click(function(){
       alert("CANCEL");
       $("#dialog_box").hide();
    });
</script>

暫無
暫無

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

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