0

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

<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,我该怎么做,它不会让表单提交?

提前致谢

4

1 回答 1

0

you have a bad script that will validate the form immediately after loading the webpage. it must be executed upon submitting the form:

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

<script>

$('#quiz').submit(function(e)
{
    e.preventDefault();
    // validate form
    // if success,
    $(this).submit();
});
</script>
于 2012-11-12T11:28:59.053 回答