简体   繁体   English

在javascript中询问是/否问题的代码

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

I could only find the function confirm() that gives OK/Cancel buttons.我只能找到提供确定/取消按钮的功能confirm() Is there any way to give Yes/No buttons?有没有办法给是/否按钮?

Javascript offers 3 modal boxes. Javascript 提供了 3 个模态框。 prompt , confirm and alert . promptconfirmalert None of those satisfy your request.这些都不能满足您的要求。

There are a plethora of js modal popup solutions.有大量的 js 模态弹出解决方案。 Here's an example.这是一个例子。

No.没有。

Instead you could use a in browser modal popup.相反,您可以使用浏览器模式弹出窗口。

Like everyone else above says, you're stuck with OK/Cancel using confirm() .就像上面的其他人说的一样,您使用confirm()被困在 OK/Cancel 上。

I would like to recommend this jQuery plugin though: jqModal .我想推荐这个jQuery插件: jqModal I've used it on 3 recent projects and it has worked great for each one.我最近在 3 个项目中使用了它,并且对每个项目都非常有效。 Specifically check out this example:具体看看这个例子:

6). 6). FUN!好玩! Overrides -- a.覆盖 - a. view (alert), b.查看(警报),b。 view (confirm) It is now time to show a real-world use for jqModal -- overriding the standard alert() and confirm dialogs! view (confirm) 现在是展示 jqModal 的实际用途的时候了——覆盖标准的 alert() 并确认对话框! Note;注意; due to the single threaded nature of javascript, the confirm() function must be passed a callback -- it does NOT return true/false.由于 javascript 的单线程特性,confirm() 函数必须传递一个回调——它不返回真/假。

No, but there are JavaScript libraries that can accomplish this for you.不,但有一些 JavaScript 库可以为您完成此任务。 Just as an example, Ext JS can be used to create a message box dialog .例如, Ext JS可用于创建 消息框对话框

I'm a fan of jQuery UI Dialog for this sort of thing.对于这类事情,我是jQuery UI Dialog的粉丝。 Here's a sample...这是一个示例...

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

i would use sweetalert https://sweetalert.js.org/guides/ to achieve something like this我会使用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>

Use dialog box to display yes or no使用对话框显示是或否

           <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