How can JQuery validate() be executed before onClick="functionname()";
A form, I use AJAX to transfer the data. The AJAX is triggered by onClick="", I also use JQuery validate(). J开发者_运维百科Query validate() is executed after the onclick=“function()", I would like the JQuery validate() is executed before the onclick="functionname()", how to do it?
<?php $addtext=<<<html
<a id="give_comment" href="javascript:void(0)">Supplement</a>
<div id="comment_text" name="comment_text" style="display:none">
<form id="add_text" action="nofile.php" method="post" onsubmit="return false;">
<textarea id="giver_comment_text" name="giver_comment_text" rows="6" cols="70" class="font1"></textarea><br/>
<input id="giver_submit_comment" type="submit" value="Submit" onclick="validateDetail()" /></form></div>
<br/>
html;
echo $addtext;
?>
Javascript code:
function validateDetail()
{
$('#add_text').validate({
rules:{
giver_comment_text:{
required:true,
minlength:50,
maxlength:600
}
},
});
var detail=$('#giver_comment_text').val();
//$.post(The content of AJAX);
}
You can remove the function from the onClick
handler, and use a submitHandler
. Read the documentation, and here's the sample they provide there:
$(".selector").validate({
submitHandler: function(form) {
// do other stuff for a valid form
form.submit();
}
})
So all you have to do is call function()
inside the submitHandler
after the form has been validated.
精彩评论