Skip to content

Instantly share code, notes, and snippets.

@callblueday
Created August 19, 2013 01:34
Show Gist options
  • Save callblueday/6265101 to your computer and use it in GitHub Desktop.
Save callblueday/6265101 to your computer and use it in GitHub Desktop.
return false is better because it prevents the default behavior of the event and also prevents the page from bubbling up. Note that this is only correct in jQuery event handlers.
//使用return false来禁用 form的默认submit事件。
<form method="POST" action="" id="search-form">
<input type="text" name="keywords" />
<input type="submit" value="Search" id="sButton" onclick="loadXMLDoc('file.xml')" />
</form>
<script>
window.onload = function() {
document.getElementById("search-form").onsubmit = function() {
loadXMLDoc('file.xml');
return false;
};
};
</script>
@callblueday
Copy link
Author

return false is better because it prevents the default behavior of the event and also prevents the page from bubbling up. Note that this is only correct in jQuery event handlers.

@callblueday
Copy link
Author

$( "form" ).on( "submit", function( event ) {
event.preventDefault();
});

用jqeury也可以这么干

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment