Validator提供错误消息,但仍然允许表单提交

时间:2012-11-09 09:55:44

标签: javascript jquery forms validation

我在我的网站上进行了小测验,如果选择了正确的答案,我需要检查表格提交。如果某些所需的字段尚未填写,则不会提交表单并在测验部分中给出错误答案的错误,这是我的脚本;

<script type="text/javascript">// <![CDATA[

$.extend($.validator.messages, {
  required: "this is required",
  email: "enter valid email"
});

$.validator.addMethod("equalValue", function(value, element, param) { 
    var elem_name = $(element).attr('name');
    value = $('input:radio[name="'+elem_name+'"]:checked').val();
    return(value == param);
}, 'Incorrect answear');


$(document).ready(function() {
$("#form").validate({
ignoreTitle: true,
 rules: {
    gender: "required ",
    quiz1: {required: true, equalValue: "A"},
    quiz2: {required: true, equalValue: "A"},
    quiz3: {required: true, equalValue: "C"},
    quiz4: {required: true, equalValue: "B"},
    quiz5: {required: true, equalValue: "C"},
    quiz6: {required: true, equalValue: "B"},
  }
 });

});
// ]]></script>

如果它返回false我该如何制作它不会让表单提交?

提前致谢

1 个答案:

答案 0 :(得分:0)

你有一个糟糕的脚本,它会在加载网页后立即验证表格。它必须在提交表格后执行:

<form id="quiz">
</form>

<script>

$('#quiz').submit(function(e)
{
    e.preventDefault();
    // validate form
    // if success,
    $(this).submit();
});
</script>