0

I am using block ui to block the page and capture some information:

On the message div, I have 2 buttons. OK and Cancel.

If I set the click events on the page load:

$('#title-picker input[name=ok]').click(ok);
$('#title-picker input[name=cancel]').click(cancel);

The events are not triggered after calling $.blockUI. However if I use the .live method instead it works as intended.

$('#title-picker input[name=ok]').live('click',ok);
$('#title-picker input[name=cancel]').live('click',cancel);

I assume the mechanism it uses removes and adds the div to the DOM, and this must be what is detaching the original event handlers. I've used earlier versions of block ui before and it hasn't done this. And I can't see anything obvious in the documentation.

So, is my reasoning correct?

And are there any downsides to using .live, i.e. is there a better workaround to what I have above?

4

1 回答 1

0

你可以使用委托而不是现场

$('#title-picker').delegate('input', 'click', function(){
  if (this.name == 'ok') {
    ok();
  } else {
    cancel();
  }
//or even:  

  // window[this.name]();

  //if needed use the scope where your functions are defined 
  //instead of the window element
});

ps:在文档中提到:

BlockUI 插件的第 2 版有哪些变化?

    Elements are no longer removed from the DOM when unblocking
于 2011-11-08T10:53:43.457 回答