I have a simple form.
Input fields, checkboxes, radio buttons and finally SUBMIT button.
I use jQuery to perform AJAX validation, however when a user presses ENTER inside the input field it submits the form!
How do I stop this from happening on this form (not all input fields)?
suppose your inputs for which you don't want to submit on enter has class noSubmit
then use this code:
$(function(){
$("input.noSubmit").keypress(function(e){
var k=e.keyCode || e.which;
if(k==13){
e.preventDefault();
}
});
});
You can use the onsubmit
handler to trap form submission and perform validation:
<form onsubmit="return validate();">
...
</form>
<script type="text/javascript">
function validate() {
// return true to submit form or false to stop
}
</script>
精彩评论