在给定的expamle中,如果我们提交表单时未选择任何值,则会出现错误消息,此后,当我们移至下一页并进入上一页时,仍然会出现错误消息显示,我们该如何解决?
HTML
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
<a href="#second" class="ui-btn-right">Next</a>
</div>
<div data-role="content">
<form id="form1" action="">
<select id="select_list" name="select_list">
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
<div data-role="page" id="second">
<div data-theme="a" data-role="header">
<a href="#index" class="ui-btn-left">Back</a>
<h3>
Second Page
</h3>
</div>
<div data-role="content">
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
JavaScript
$('#index').live('pageshow', function(e, data) {
$.validator.addMethod("valueNotEquals", function(value, element, arg) {
return arg != value;
}, "");
$("#form1").validate({
rules: {
select_list: {
valueNotEquals: "default"
},
},
messages: {
select_list: {
valueNotEquals: "You must select a value"
}
},
submitHandler: function(form) {
alert($('#form1').valid());
form.submit();
}
});
});