admin管理员组文章数量:1425783
function myfunction(){
$('button').click(function(){
alert();
});
}
myfunction();
$('#x').off('click', myfunction);//click disable myfunction();
<button></button>
<span id='x'></span>
I need to disable function, I have try jquery off, but its not working
can anyone tell me how? and how to enable again?
function myfunction(){
$('button').click(function(){
alert();
});
}
myfunction();
$('#x').off('click', myfunction);//click disable myfunction();
<button></button>
<span id='x'></span>
I need to disable function, I have try jquery off, but its not working
can anyone tell me how? and how to enable again?
Share Improve this question edited Sep 21, 2014 at 20:14 Ben asked Sep 21, 2014 at 20:07 BenBen 2,5648 gold badges39 silver badges63 bronze badges 6- 2 What do you want exactly? It is not clear from your description. – Apul Gupta Commented Sep 21, 2014 at 20:09
- when #x click, disable myfunction() – Ben Commented Sep 21, 2014 at 20:10
-
What is
'#x'
, and which function exactly are you trying to disable? Are you trying to toggle the 'click' handler function? – ne1410s Commented Sep 21, 2014 at 20:11 - I guess you want when #x clicked, button click should not work? – Apul Gupta Commented Sep 21, 2014 at 20:11
- yes, #x clicked, myfunction(); will disable – Ben Commented Sep 21, 2014 at 20:12
3 Answers
Reset to default 3What, exactly, do you expect this to do?:
$('#x').off('click', myfunction);
Assuming for a moment that #x
is a subset of button
, you never actually set myfunction
as a handler on button
in the first place. You set an anonymous function:
$('button').click(function(){
alert();
});
If you want to "disable" that function (that is, remove the click handler from your element), then make it a non-anonymous function and remove the handler:
function myfunction () {
alert();
}
$('button').on('click', myfunction);
$('#x').off('click', myfunction);
Or, if you're just going to do this once and trying to set the handler on all elements except that one, just omit it from your selector:
$('button').not('#x').on('click', myfunction);
Add one variable like isDisabled = false; Check that variable at the begining of the MyFunction(). And set that variable to true when #x clicked.
There is a bug in Internet Explorer with function attr() e prop()... when use disable attribuite.. See here for plete solution: https://lentux-informatica./bug-jquery-disable-e-internet-explorer-soluzione/
本文标签: javascriptJquery disable functionStack Overflow
版权声明:本文标题:javascript - Jquery disable function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745450947a2658880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论