[英]JQuery onclick condition stop link?
I have a link: 我有一个链接:
Link 链接
using:: 使用::
$('#myID').click(function(){
if($("#myCheckbox").is(":checked")) {
alert('Yes it is...'); //continue as normal
}
else {
alert('Not checked');
//exit do not follow the link
}
...
so is //exit do not follow the link possible? 所以是//退出不要跟随链接吗?
Try using event.preventDefault()
尝试使用
event.preventDefault()
$('#myID').click(function(e) {
if ($("#myCheckbox").is(":checked")) {
alert('Yes it is...');
}
else {
alert('Not checked');
e.preventDefault(); // this prevents the standard link behaviour
}
}
Simple: 简单:
$('#myID').click(function (e) {
...
} else {
alert('Not checked');
e.preventDefault();
}
You can also use return false
, but this will also stop the propagation of the click event (which might be undesired). 您也可以使用
return false
,但这也会停止click事件的传播(这可能是不受欢迎的)。
just use return false;
只使用
return false;
when you want to stop the action at a specific point 当您想要在特定点停止操作时
Return false in your else condition. 在你的其他情况下返回false。
$('#myID').click(function(){
if($("#myCheckbox").is(":checked")) {
alert('Yes it is...'); //continue as normal
}
else {
alert('Not checked');
return false;
}
You can use event.preventDefault() 你可以使用event.preventDefault()
Let your click-function receive the event as a parameter. 让您的click-function接收事件作为参数。 Then you can do event.preventDefault() when you don't want to follow the link.
然后,当您不想关注链接时,可以执行event.preventDefault()。
$('#myID').click(function(event){
if($("#myCheckbox").is(":checked")) {
alert('Yes it is...'); //continue as normal
}
else
{
alert('Not checked');
//exit do not follow the link
event.preventDefault();
}
});
You can pass in the event, and override the default behavior with the following: 您可以传入事件,并使用以下内容覆盖默认行为:
$('#myID').click(function(e) {
e.preventDefault();
//exit do not follow the link
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.