每个函数调用都没有更新回调内的值

时间:2013-02-27 16:40:19

标签: javascript jquery

我有一个非常简单的功能,如下所示。这会在确认他们想要执行的操作后删除用户单(请注意自定义confirm)。在确认的回调函数内,id第一次正确设置,但在此之后永远不会更新。

如果我在console.log函数内deleteSlipid总是正确的。如果我在回调中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);
  }
}

deleteSlip是按钮上的onclick HTML属性。像这样:

onclick="deleteSlip(1234)"

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

我之前发过这个问题,被告知这个问题太模糊了。请让我知道你需要看到什么来帮助我解决这个问题。 &#34;做东西&#34; area有一个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所见过的内容。我没有unbind正确的东西?

2 个答案:

答案 0 :(得分:1)

这里有一个范围问题。 id deleteSlip()中定义,而不是在回调中定义。因此,您应该将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);
  }
}

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

答案 1 :(得分:-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 
            }
        }); }