jQuery return onClick
I want to know if is it possible to do the same javascript code开发者_JAVA百科 with Jquery:
<input type="button" value="Save" onclick="javascript:return CheckPasswords()" />
What it does is return true or false on user click.
With Jquery I would write:
$(function()
{
$("#btnSalvar").click(function(){
if([...])
{
return false;
}
return true;
});
}
But i dont know in case return false it would have the same effect.
**UPDATED:**Solution is
$("btnSalvar").click(function(e){
if([...])
{
e.preventDefault();
}
});
thk you all!
Since the parameter to the click
function is a closure, the return value is lost. Try something like this:
$(function(){
$("#btnSalvar").click(function(){
window.val = [...];
}
});
\\ Do what you want with val here
Of course, if possible avoid assigning variables to the global namespace, but without further information on what you're trying to do, it's the only thing that you can guarantee will work.
it returning false should have the same result whether done in javascript or jquery
$('#btnSalvar').click(function(){ return CheckPasswords(); });
should operate the same way as your onclick event in jquery
This sounds like you're asking what "return false" does here, is that right? "return false" will prevent action on the click - so it's essentially the same as event.preventDefault() and event.stopPropagation(). Essentially, nothing will happen at all.
精彩评论