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