简体   繁体   English

我们可以用自定义div和消息创建一个Javascript确认框吗

[英]Can we create a Javascript confirm box with custom div and message

I need a javascript confirm box with customized design and dynamic message . 我需要一个带有自定义设计和动态消息的JavaScript 确认框

My problem is: I need a function to call in that call how can i receive the result of that popup; 我的问题是:我需要一个函数来调用该调用,我该如何接收该弹出窗口的结果? ( like yes or no). (如是或否)。 Is that possible? 那可能吗?

Code

if (confirm('Are you sure?')){
    //Do the process
}
else{ 
    //Show Error 
}  

I want to override that confirm with my custom style 我想用我的自定义样式覆盖该确认

How about making a hidden div that will allow the user to respond 如何制作一个隐藏的div以允许用户响应

    <div id="confirm" class="noshow">
      <div  id="messageContent" class="message"></div>
      <div id="okButton" onClick="doConfirm(true)" class="button"></div>
      <div id="cancelButton" onClick="doConfirm(false)" class="button"></div>
    </div>

now you can set the message easily with getElementById('messageContent') similarly you can now handle the ok and cancel actions with doConfirm(true) and doConfirm(false) functions 现在您可以使用getElementById('messageContent')轻松设置消息,类似地,您现在可以使用doConfirm(true)doConfirm(false)函数处理okcancel操作

in a simple manner your javascript will be something like 以简单的方式,您的javascript就像

doConfirm(flag){
  return flag;
 }

and have css like 并有CSS喜欢

.noshow{display:none;}

now you can build the html from javascript at runtime and attach the event handlers but this is about the simplest I could think of. 现在您可以在运行时从javascript构建html并附加事件处理程序,但这是我能想到的最简单的方法。

For me this solution much better: 对我来说,这种解决方案更好:

html: 的HTML:

<div id='modal_dialog'>
        <div class='title'></div>
        <input type='button' value='yes' id='btnYes' />
        <input type='button' value='no' id='btnNo' />
</div>

js: js:

function dialog(message, yesCallback, noCallback) {
    $('.title').html(message);
    var dialog = $('#modal_dialog').dialog();

    $('#btnYes').click(function() {
        dialog.dialog('close');
        yesCallback();
    });

    $('#btnNo').click(function() {
        dialog.dialog('close');
        noCallback();
    });
}

use: 采用:

    dialog('Are you sure you want to do this?',
        function() {
            //If yes do something
            console.log("yes");
        },
        function() {
            //If no do something else
            console.log("no");
        }
    );

don't forget to add jquery if you didn't. 如果没有,请不要忘记添加jquery。

based on alnorth29 answer to this question 根据alnorth29回答这个问题

You can use callbacks to do this 您可以使用回调执行此操作

Confirm handler, to be added in your global javascript file 确认处理程序,将其添加到全局javascript文件中

function ConfirmInDiv(message, okcallback, cancelcallback) {
  $('#confirmation-holder').show();
  $("#confirmation_message").text(message);
  $('#Confirmation_Ok').unbind('click');
  $('#Confirmation_Ok').click(okcallback);
  $('#Confirmation_Cancel').unbind('click');
  $('#Confirmation_Cancel').click(cancelcallback);
}

function HideConfirmDiv() {
  $('#confirmation-holder').hide();
}

HTML, should reside in your layout file: HTML,应位于您的布局文件中:

<div id="confirmation-holder" style="display:none">
    <div id="confirmation_message"></div>
    <a id="Confirmation_Ok" href="#" class="button">OK</a>
    <a id="Confirmation_Cancel" href="#" class="button light-button">Cancel</a>
</div>

And wherever you want to call your javascript confirm: 以及无论您想调用javascript的何处,请确认:

Instead of this 代替这个

function delete()
{
     if(confirm("Delete?")
     {
         console.log('deleted');
     }
     else
     {
         console.log('delete cancelled');
     }
}

do this 做这个

function delete()
{
    function ok()
    {
         console.log('deleted');
         HideConfirmDiv();
    }
    function cancel()
    {
         console.log('delete cancelled');
         HideConfirmDiv();
    }
    ConfirmInDiv('Delete?', ok, cancel);   
}

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

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