jQuery .off,然后将其还原为on [英] Jquery .off and then restore it to on
问题描述
当我想从提交按钮中删除点击处理程序时,我正在使用
When i want to remove the click handler from my submit button,I am using
$(#submitBtn).off();//works fine
但是,当我想恢复到以前的状态时,.on()
不起作用.
But,when I want to restore back to its previous stage,.on()
doesnt work.
if(flag){
$(#submitBtn).off();//works fine
$.ajax({
type: 'POST',
url: 'someservice.php',
data: {email:email,name:name,text:text},
success: function (data) {
$('#submitBtn').on();//does not work
});//ajax end
}
推荐答案
.off()
不带参数调用.off()会删除所有附加到元素的处理程序.通过提供事件名称,名称空间,选择器或处理程序函数名称的组合,可以删除元素上的特定事件处理程序.
Calling .off() with no arguments removes all handlers attached to the elements. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names.
.on()
为选定的元素附加一个或多个事件的事件处理函数.并且必须有参数-
Attach an event handler function for one or more events to the selected elements. And it must have arguments -
.on( events [, selector ] [, data ], handler )
建议您使用按钮禁用逻辑来代替绑定和解除绑定:.prop('disabled')
$('#submitBtn').prop('disabled',true) //disable
$('#submitBtn').prop('disabled',false) //enable
注意:ID选择器写为$('#submitBtn')
而不是$(#submitBtn)
Note : The ID selector is written as $('#submitBtn')
and not $(#submitBtn)
这篇关于jQuery .off,然后将其还原为on的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!