正如我在标题中提到的,我想防止当我触发另一个事件时触发另一个事件:
让我们举个例子:
$(function() {
$("#security_code").bind('change', function () {
// Do validation and show errors
});
$("#reloadCaptcha").bind('click', function () {
// generate new captcha and clear 'secuirity code' field
// clear the field so the user will enter the new generated security code
document.getElementById('security_code').value = '';
});
});
目的:防止单击“#reloadCaptcha”时触发“change”事件。
当我单击链接(第二个事件)时,第一个事件也会触发(因为“更改”激活)。我希望在实际单击链接时停止所有“更改”事件。我希望我说得足够清楚。有人有什么想法吗?
是的,我尝试过 jQuery 文档中的 unbind、stopPropagation 和其他函数,但它不起作用。
请注意,我只想在单击该特定链接时卡住“更改”。在其他情况下,它应该始终有效。
提前谢谢大家!我希望有人可以帮我澄清这一点。
最佳答案
$(function() {
$("#security_code").bind('change', function () {
// If #security_code is empty do nothing
if ($('#security_code').val() == '')
{
return;
}
// Otherwise validate...
});
$("#reloadCaptcha").bind('click', function () {
// generate new captcha and clear 'secuirity code' field
// clear the field so the user will enter the new generated security code
$('#security_code').val('');
});
});
关于JQuery:采取特定操作时防止触发其他事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371982/