<script type="text/javascript">
function add() {
var $password = $("#password").val();
var $repassword = $("#repassword").val();
if($password != $repassword) {
alert("两次密码不一致!");
return false;
}
</script>
</head>
<body>
<s:form οnsubmit="return add()" action="addAction">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码" id="password"></s:password>
<s:password name="repassword" label="确认密码" id="repassword"></s:password>
<s:select name="sex" list="{'男','女'}" label="性别" headerValue="性别"></s:select>
<s:textfield name="age" label="年龄"></s:textfield>
<s:textfield name="address" label="地址"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
function add() {
var $password = $("#password").val();
var $repassword = $("#repassword").val();
if($password != $repassword) {
alert("两次密码不一致!");
return false;
}
</script>
</head>
<body>
<s:form οnsubmit="return add()" action="addAction">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码" id="password"></s:password>
<s:password name="repassword" label="确认密码" id="repassword"></s:password>
<s:select name="sex" list="{'男','女'}" label="性别" headerValue="性别"></s:select>
<s:textfield name="age" label="年龄"></s:textfield>
<s:textfield name="address" label="地址"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
关键点在于οnsubmit="return add()",记住是return add(),不是简单的add().以前一直以为在这种地方写不写return都是一样的,现在才知道原来只有写了这个return才能取到这个false,虽然搞了一晚上,不过还是觉得值得!