Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-自定义确认对话框-替换JS确认_Javascript - Fatal编程技术网

Javascript-自定义确认对话框-替换JS确认

Javascript-自定义确认对话框-替换JS确认,javascript,Javascript,这可能是一个简单的答案- 在我的JS中,我用自己的函数替换了JS的确认函数。这基本上是这样的: function confirm(i){ var options = '<br/><br/><input class="button1" value="Yes" type="button" onclick="return true"> <input class="button1" value="No" type="button" onclick="re

这可能是一个简单的答案-

在我的JS中,我用自己的函数替换了JS的确认函数。这基本上是这样的:

function confirm(i){
    var options = '<br/><br/><input class="button1" value="Yes" type="button" onclick="return true"> <input class="button1" value="No" type="button" onclick="return false">';
    $('#text').html(i+options);
    $('#confirmDiv').fadeIn('fast');
}
如何获得直接返回值的确认?我假设它返回{this.value}左右


谢谢

您的问题是您的自定义确认不是模态的。这意味着当显示确认时,代码将继续运行。您没有机会在
confirm()
中等待用户的选择并从中返回

据我所知,没有办法用Javascript模拟模式确认对话框的行为(非标准对话框除外)


通常的方法是向每个按钮的
click
事件添加
function(){…}
回调,并在其中执行“ok”单击应该执行的操作

我解决这个问题的方法是向对象中添加一些任意数据,并在单击时检查这些数据。如果存在,则按正常方式继续该函数,否则用是/否确认(在我的情况下,使用jqtools覆盖)。如果用户单击“是”-在对象中插入数据,则模拟再次单击并擦除数据。如果他们单击“否”,只需关闭覆盖

以下是我的例子:

$('button').click(function(){
    if ($(this).data('confirmed')) {
        // Do stuff
    } else {
        confirm($(this));
    }
});
这就是我覆盖确认函数所做的(使用jquery工具覆盖):

window.confirm=功能(obj){
$('#dialog').html('\
\
证实\
你确定吗?

\ \ 对\ 没有\

\ ').overlay().load(); $('button[name=confirm]')。单击(函数(){ 如果($(this.val()=='yes'){ $('#dialog').overlay().close(); 对象数据('已确认',为真)。单击()。删除数据('已确认'); }否则{ $('#dialog').overlay().close(); } }); }
我找到了另一个被黑客攻击的解决方案来模拟Pekka提到的modale对话框웃 之前如果中断JS执行,则无需在一段时间内循环(true)。检索用户输入(单击)后,我们可以继续执行JS,同时再次使用eval调用origin方法,并将用户选择返回为boolean。 我使用jquery和notyjs创建了一个小JSFIDLE,以简单地显示我的解决方案:

下面是重要的代码:

/** confirm as noty.JS **/
var calleeMethod2 = null;
var returnValueOfConfirm = null;
var args = null;
var calleeMethod = null;
var refreshAfterClose = null;
var savedConfirm = window.confirm;
window.confirm = function(obj) {
    // check if the callee is a real method
    if (arguments.callee.caller) {
        args = arguments.callee.caller.arguments;
        calleeMethod = arguments.callee.caller.name;
    } else {
        // if confirm is called as if / else - rewrite orig js confirm box
        window.confirm = savedConfirm;
        return confirm(obj);
    }
    if (calleeMethod != null && calleeMethod == calleeMethod2) {
        calleeMethod2 = null;
        return returnValueOfConfirm;
    }
    noty({
        text: obj,
        buttons: [{
          text: 'Yes',
          onClick: function($noty) {
            $noty.close();
            noty({
              text: 'YES',
              type: 'success'
            });
          }
        }, {
          text: 'No',
          onClick: function($noty) {
            $noty.close();
            noty({
              text: 'NO',
              type: 'error'
            });
          }
        }]
    });
    throw new FatalError("!! Stop JavaScript Execution !!");
}

function runConfirmAgain() {
  calleeMethod2 = calleeMethod;
  // is a method
  if (calleeMethod != null) {
    var argsString = $(args).toArray().join("','");
    eval(calleeMethod2 + "('" + argsString + "')");
  } else {
    // is a if else confirm call
    alert('confirm error');
  }
}

confirmDiv的内容是什么?不太可能。通常,
确认
会等待用户提供答案。由于Javascript没有等待/休眠,您必须使用回调来解决此问题。可能与另一个示例重复如何在此处执行此操作:
window.confirm = function(obj){
    $('#dialog').html('\
        <div>\
            <h2>Confirm</h2>\
            <p>Are you sure?</p>\
            <p>\
                <button name="confirm" value="yes" class="facebox-btn close">Yes</button>\
                <button name="confirm" value="no" class="facebox-btn close">No</button>\
            </p>\
        </div>').overlay().load();
    $('button[name=confirm]').click(function(){
        if ($(this).val() == 'yes') {
            $('#dialog').overlay().close();
            obj.data('confirmed', true).click().removeData('confirmed');
        } else {
            $('#dialog').overlay().close();
        }
    });
}
/** confirm as noty.JS **/
var calleeMethod2 = null;
var returnValueOfConfirm = null;
var args = null;
var calleeMethod = null;
var refreshAfterClose = null;
var savedConfirm = window.confirm;
window.confirm = function(obj) {
    // check if the callee is a real method
    if (arguments.callee.caller) {
        args = arguments.callee.caller.arguments;
        calleeMethod = arguments.callee.caller.name;
    } else {
        // if confirm is called as if / else - rewrite orig js confirm box
        window.confirm = savedConfirm;
        return confirm(obj);
    }
    if (calleeMethod != null && calleeMethod == calleeMethod2) {
        calleeMethod2 = null;
        return returnValueOfConfirm;
    }
    noty({
        text: obj,
        buttons: [{
          text: 'Yes',
          onClick: function($noty) {
            $noty.close();
            noty({
              text: 'YES',
              type: 'success'
            });
          }
        }, {
          text: 'No',
          onClick: function($noty) {
            $noty.close();
            noty({
              text: 'NO',
              type: 'error'
            });
          }
        }]
    });
    throw new FatalError("!! Stop JavaScript Execution !!");
}

function runConfirmAgain() {
  calleeMethod2 = calleeMethod;
  // is a method
  if (calleeMethod != null) {
    var argsString = $(args).toArray().join("','");
    eval(calleeMethod2 + "('" + argsString + "')");
  } else {
    // is a if else confirm call
    alert('confirm error');
  }
}