JavaScript 位运算:
$(document).ready(function()
{
$('#form').submit(function(e)
{
e.preventDefault();
var $form = $(this);
// check if the input is valid
if(! $form.valid()) return false;
$.ajax(
{
type:'POST',
url:'add.php',
data:$('#form').serialize(),
success:function(response)
{
$("#answers").html(response);
}
});
})
});
HTML 部分:
<input type="text" name="answer[1]" class="required" />
<input type="text" name="answer[2]" class="required" />
这是我正在尝试使用的代码。我的想法是在使用Ajax发送表单之前验证所有输入。我已经尝试了许多版本,但每次都会提交,即使表单没有完全填写。我所有的输入都是“required”类。有人能看出我做错了什么吗?
此外,我依赖于基于类的要求,因为我的输入名称是用PHP生成的,所以我无法确定我得到了什么名称[id]或输入类型。
我在“页面”中显示/隐藏问题。
<input type="button" id="next" onClick="toggleVisibility('form3')" class="next-btn"/>
JS:
function toggleVisibility(newSection)
{
$(".section").not("#" + newSection).hide();
$("#" + newSection).show();
}
beforeSubmit
作为$.ajax的有效选项。您可能希望使用最新版本的jquery。话虽如此,Darin的答案是正确的。话虽如此,如果您无法执行他建议的操作,则应将beforeSubmit的最后一行替换为Fliespl提供的代码。您需要检查.validate()
的返回值,并相应地继续或取消ajax请求。 - Amith George