2

我有一个非常简单的功能,如下所示。这会在确认这是他们想要做的事情后删除用户单据(注意 custom confirm)。在确认的回调函数中,id第一次设置正确,但之后永远不会更新。

如果我console.logdeleteSlip函数里面,id总是正确的。如果我console.log在回调内部,它总是id我第一个发送它。当我调试 javascript 时,我看到了同样的事情。该值第一次是正确的,但之后永远不会更新。

我需要做什么才能让回调函数中的值在更新时更新deleteSlip

function deleteSlip(id){
    //  id is correct right here
    confirm('This will delete your slip.  Are you sure you want to do this?', function(confirmed){
        //  id here is always the first value from deleteSlip, and never changes
        if (confirmed)
        {
          // do stuff 
        }
    });
}

被调用是自定义的confirm,定义如下:

window._originalConfirm = window.confirm;
window.confirm = function(text, cb) {
  bootStrapConfirm = function() {
    if(! $.fn.modal.Constructor)
      return false;
    if($('#windowConfirmModal').length == 1)
      return true;
    $('body').append(' \
    <div id="windowConfirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
      <div class="modal-body"> \
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
        <p> alert text </p> \
      </div> \
      <div class="modal-footer"> \
        <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">No</button> \
        <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Yes</button> \
      </div> \
    </div> \
    ');
    function unbind() { 
      $("#windowConfirmModal .btn-primary").unbind('click', confirm);
      $("#windowConfirmModal .btn-danger").unbind('click', deny);
    }
    function confirm() { cb(true); delete cb;}
    function deny() { cb(false); delete cb;}
    $("#windowConfirmModal .btn-primary").bind('click', confirm);
    $("#windowConfirmModal .btn-danger").bind('click', deny);
    return true;
  }
  if ( bootStrapConfirm() ){
    $('#windowConfirmModal .modal-body p').text(text);
    $('#windowConfirmModal').modal();
  }  else {
    console.log('bootstrap was not found');
    window._originalConfirm(text);
  }
}

deleteSliponclick按钮上的 HTML 属性。像这样:

onclick="deleteSlip(1234)"

再次重申,id内部deleteSlip是正确的。id回调的内部confirm是它首先得到的,并且永远不会改变。回调可能是异步的,但我在任何给定时间只调用一次。 deleteSlip在按钮单击时触发。

我之前发布了这个问题,并被告知它太模棱两可了。请让我知道你需要看到什么来帮助我解决这个问题。“do stuff”区域有一个 ajax 调用,但在此之前的值是错误的(因此不向没有阅读整个问题的人显示它)。

更新:看起来这是自定义的问题confirm。我将自定义更改confirm为以下内容:

window.confirm = function(text, cb) {
  bootStrapConfirm = function() {
    if(! $.fn.modal.Constructor)
      return false;
    if($('#windowConfirmModal').length == 1)
      return true;
    $('body').append(' \
      <div id="windowConfirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
      <div class="modal-body"> \
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
        <p> alert text </p> \
      </div> \
      <div class="modal-footer"> \
        <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">No</button> \
        <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Yes</button> \
      </div> \
      </div> \
    ');
    return true;
  }
  if ( bootStrapConfirm() ){
    function confirm() { cb(true); }
    function deny() { cb(false); }
    $("#windowConfirmModal .btn-primary").unbind('click', confirm);
    $("#windowConfirmModal .btn-danger").unbind('click', deny);
    $("#windowConfirmModal .btn-primary").bind('click', confirm);
    $("#windowConfirmModal .btn-danger").bind('click', deny);
    $('#windowConfirmModal .modal-body p').text(text);
    $('#windowConfirmModal').modal();
  }  else {
    console.log('bootstrap was not found');
    window._originalConfirm(text);
  }
}

现在,当confirm被调用时,它会调用id它所见过的所有 's。我不unbind正确的东西?

4

2 回答 2

1

你在这里有一个范围问题。iddeleteSlip()中定义,而不是在回调中。因此,您应该将 id 作为参数传递给回调。

在内置的 jquery 中,这将通过在回调函数被提供给confirm()之前添加参数来完成。不知道你会怎么做。

在你的情况下,我会这样做:

function deleteSlip(id){
  //  id is correct right here
  confirm('This will delete your slip.  Are you sure you want to do this?', id, function(cb_id, confirmed){
      //  id here is always the first value from deleteSlip, and never changes
      if (confirmed)
      {
        // do stuff 
      }
  });
}

和:

window._originalConfirm = window.confirm;
window.confirm = function(text, id, cb) {
  bootStrapConfirm = function() {
    if(! $.fn.modal.Constructor)
      return false;
    if($('#windowConfirmModal').length == 1)
      return true;
    $('body').append(' \
      <div id="windowConfirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
      <div class="modal-body"> \
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
        <p> alert text </p> \
      </div> \
      <div class="modal-footer"> \
        <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">No</button> \
        <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Yes</button> \
      </div> \
    </div> \
    ');
    function unbind() { 
      $("#windowConfirmModal .btn-primary").unbind('click', confirm);
      $("#windowConfirmModal .btn-danger").unbind('click', deny);
    }
    function confirm() { cb(id, true); delete cb;}
    function deny() { cb(id, false); delete cb;}
    $("#windowConfirmModal .btn-primary").bind('click', confirm);
    $("#windowConfirmModal .btn-danger").bind('click', deny);
    return true;
  }
  if ( bootStrapConfirm() ){
    $('#windowConfirmModal .modal-body p').text(text);
    $('#windowConfirmModal').modal();
  }  else {
    console.log('bootstrap was not found');
    window._originalConfirm(text);
  }
}

免责声明:尚未运行代码,因此可能存在一些错误。但这个想法应该是合理的。

于 2013-02-27T16:52:20.790 回答
-1

您应该将 id 显式传递给回调函数。

    function deleteSlip(id){
        //  id is correct right here
        confirm('This will delete your slip.  Are you sure you want to do this?',        function(confirmed,id){
            var newId = id;
            alert(newId);
            //  id here is always the first value from deleteSlip, and never changes
            if (confirmed)
            {
              // do stuff 
            }
        }); }
于 2013-02-27T16:45:42.053 回答