0

我正在尝试使用验证插件使用 jQuery 验证表单,但似乎无法弄清楚如何设置多个规则来验证多个字段。

这是我的测试脚本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){       
        $("#test").click(function () {              
            $('#test_form').validate({
                rules: {
                    firstName: {
                        required: true
                    },
                    surname: {
                        required: true
                    }
                },
                success: function() {
                    alert('it works?');
                }
            });
        });
    });
</script>
</head>
<body>
<form id="test_form" action="" method="post">
    <fieldset>
        <legend>Create an Account</legend>
        <label for="firstName">First Name</label><br/>
        <input id="firstName" name="firstName" type="text" /><br/><br/>
        <label for="surname">Surname</label><br/>
        <input id="surname" name="surname" type="text" /><br/><br/>
        <a href="#" id="test">try me</a>
    </fieldset>
</form>
</body>
</html>

任何关于我如何设法搞砸的想法,将不胜感激!

谢谢!

4

2 回答 2

1

您需要$("#test_form").validate(...)在点击链接之前致电。现在发生的是当点击链接时,验证被添加到表单中,但没有经过测试。

尝试这个:

var $form = $('#test_form');

$form.validate({
    rules: {
        firstName: {
            required: true
        },
        surname: {
            required: true
        }
    },
    success: function() {
        alert('it works?');
    }
});

$("#test").click(function(e) {
    e.preventDefault();
    $form.submit();
});
于 2012-05-02T18:34:11.317 回答
0
$(document).ready(function () {
    $('#Invoice').validate( {
        rules: {
            code: {
                required: true,
                int: true
            },

            price: {
                required: true,
                int: true
            }
        }
    });
});
于 2015-04-24T02:20:10.670 回答