开发者

Preventing hardcoded javascript event

<select onchange="window.location='/staff/add_combo.php?page='+this[this.selectedIndex].value+'&amp;ipp=2';return false" class="paginate">

Is开发者_Python百科 there any way to prevent onchange event on this element?


Do you just want to remove the handler permanently or temporarily?

//grabs first select on page
var mySelect = document.getElementsByTagName('select')[0];

// permanently
mySelect.onchange = function(){};//noop

//temporarily
var toggle = true;
var orig  = mySelect.onchange;
mySelect.onchange = function(){
  if(toggle) toggle = false;
  else orig.call(mySelect);
};

The permanent version just blows out the existing handler.

The temporary version saves the original onchange handler and calls it depending on some other state in the application (in this case the "toggle" variable).


You can't prevent the event from firing on a single occasion, but you can unset it by assigning null to the element's onchange property:

document.getElementById("mySelect").onchange = null;


If by preventing you mean removing the event listener, than yes. Just use the removeEventListener() method.

document.getElementById("mySelect").removeEventListener('onchange', function(), false);

The answer By Any E should work as well, it's the traditional way of handling it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜