【发布时间】:2017-03-01 15:30:29
【问题描述】:
我是一名新手编码员,我无法使用 validator.js 将一个简单的表单放在一起
基本上,自从尝试构建表单已停止工作以来,我的猜测是'数据:$('#send-form').serialize(),'没有得到正确的路径或语法,但我可能是错的。
我一加:
$.ajax({
type: "POST",
url: "process.php",
data: $('#send-form').serialize(),
}
警报不会触发!
<form class="contact" name="contact" id="send-form" data-toggle="validator" role="form" >
<div class="form-group">
<label class="subTitle" for="name">NAME</label><br>
<input type="text" name="name" class="form-control" placeholder="Enter your full name" data-error="Please enter your name" required>
<div class="help-block with-errors"></div><br>
</div>
<div class="form-group">
<label class="subTitle" for="email">E-MAIL</label><br>
<input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" placeholder="Enter a valid email address" data-error="Invalid email address" required>
<div class="help-block with-errors"></div><br>
</div>
<div class="form-group">
<button type="submit" class="subBtn form-control">SUBMIT YOUR ENQUIRY</button>
</div>
</form>
<script>
$('#send-form').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
alert('form is not valid');
} else {
// everything looks good!
e.preventDefault();
alert('form is valid');
// your ajax
$.ajax({
type: "POST",
url: "process.php",
data: $('#send-form').serialize(),
}
});
</script>
【问题讨论】:
-
可能是因为语法错误?
-
您在 ajax 调用之后缺少一个
);,而 else 子句缺少一个}-->$('#send-form').validator().on('submit', function (e) { if (e.isDefaultPrevented()) { alert('form is not valid'); } else { // everything looks good! e.preventDefault(); alert('form is valid'); // your ajax $.ajax({ type: "POST", url: "process.php", data: $('#send-form').serialize() }); } });
标签: javascript php jquery ajax twitter-bootstrap