无法取消绑定点击事件?

时间:2013-08-02 00:40:02

标签: html forms submit jquery

我正在尝试撤消在网页标题中的链接JS文件中添加的事件处理程序/侦听器。

基本设置:

<form id="form_enter_giveaway" action="" method="post">
 <input type="hidden" name="form_key" value="04b931caff99a0a688241e6da5f09839">
 <input type="hidden" name="enter_giveaway" value="1">
 <a href="" class="rounded view submit_entry">Enter to Win! (1P)</a>
</form>

JS文件(http://www.steamgifts.com/js/header_functions.js):

$('.submit_entry, .remove_entry').click(function(){
 $('#form_enter_giveaway').submit();
 return false;
});

如果单击该链接,并且搜索对该链接的每个引用并且该表单通常似乎表明单个JS是唯一可能导致表单提交的内容,则应该运行本机。 但我在控制台上尝试了$('.submit_entry, .remove_entry') .unbind().off()die();所有有和没有'点击',每次我点击它仍然提交的链接标签。它干扰了我想要在其中运行的事件。

1 个答案:

答案 0 :(得分:2)

试试这个

var $selector = $('.submit_entry, .remove_entry');

// Binding the event using on so that it can be unbinded later
//  that triggers the submitForm handler
$selector.on('click', submitForm);

function submitForm() {
    $('#form_enter_giveaway').submit();
    return false
}

// Unbind the event using off
$selector.off('click');

// Bind the event and prevent the default action of anchor
$selector.on('click', function(e) {
    e.preventDefault();
});

<强> Check Fiddle

相关问题