简体   繁体   中英

return from parent function after click

I looked all around, but couldn't find any help with this problem.

So I have this if that is calling a function. In that I am waiting for a button press to happen. The button, therefore, should return true or false, but it's only returning to its anon-function running inside the click event and I don't know how to return from its parent function.

if ( saveConfirm() ) {
    saveFunction()
}

function saveConfirm () {
  $('#confirm').click(function() {
    $('modal').modal('hide')
    return true
  })
  $('#abort').click(function() {
    $('modal').modal('hide')
    return false
  })
}

Hope you guys understand what i mean and maybe someone can help me with how to return from the call to the if through the button that is pressed.

Click handlers execute asynchronously, you should move saveFunction call to confirm click handler. There is no way for saveConfirm return boolean value you are trying to return in click handler.

  $('#confirm').click(function() {
    $('modal').modal('hide');
    saveFunction();
  })
  $('#abort').click(function() {
    $('modal').modal('hide');
  })

You bind events in saveConfirm(). Try code below

  $('#confirm').click(function() {
    $('modal').modal('hide');
    saveFunction();
  })
  $('#abort').click(function() {
    $('modal').modal('hide');
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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