我几天来一直在解决问题。 我想在没有页面刷新的情况下使用jquery验证和ajax调用。 如果我只使用其中一个,代码工作正常但不在一起。我想要一个非常简单易懂的代码。
你可以帮助我解决问题吗?提前谢谢。
祝你好运, 姿态
**teszt.php**
<!doctype html>
<html lang="hu">
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script>
$(document).ready(function(){
$('#form').validate({
rules: {
nev: {
required: true,
minlength: 2
}
},
messages: {
nev: {
required: "We need your email address to contact you",
minlength: 2
}
},
submitHandler: function(form) {
$('#button_id').click(function(){
var nev=$("#nev").val();
var action=$("#action").val();
$.ajax({
type: "post",
url: "php.php",
data: "nev="+nev+"&action="+action,
success:function(result){
$("#nevem").html(result);
}
});
});
form.submit();
}
});
});
</script>
</head>
<body>
<form id="form">
<input type="text" id="nev" name="nev">
<input type="hidden" id="action" name="action" value="addadd">
<input type="button" id="button_id" name="button_id" value="Mentés">
</form>
<div id="nevem"></div>
</body>
</html>
**php.php**
<?php
if ($_POST["action"] == "addadd") {
echo $_POST["nev"];
} else {
echo 987;
}
?>
答案 0 :(得分:0)
这样称呼:
$('#button_id').click(function(){
$("#myform").submit(function(e) {
e.preventDefault();
}).validate({
rules: {
nev: {
required: true,
minlength: 2
}
},
messages: {
nev: {
required: "We need your email address to contact you",
minlength: 2
}
},
submitHandler: function(form) {
var nev=$("#nev").val();
var action=$("#action").val();
$.ajax({
type: "post",
url: "php.php",
data: "nev="+nev+"&action="+action,
success:function(result){
$("#nevem").html(result);
}
});
});
});
}); // no .submit() here