我有一个asp网页表单,上面有多个提交。有一个字段集我想附加验证器,但我遇到了很多问题。我无法触发提交按钮,因为无论输入是否有效,验证器错误消息都不会消失。我最初尝试将验证器附加到#emailForm-content
,但验证器根本不起作用,所以我必须使用#emailForm .validate
。
HTML
<div id="emailForm" style="display:none;">
<div id="emailForm-content">
<fieldset>
<h3>Email to a Friend:</h3>
<p>
<label for="to">To:</label>
<input id="to" class="validate" type="email" required="required" />
</p>
<p>
<label for="from">From:</label>
<input id="from" class="validate" type="email" required="required" />
</p>
<p>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
</p>
<button type="submit" id="submit">Submit</button>
<button type="button" id="cancel">Cancel</button>
</fieldset>
</div>
<div id="emailForm-thankyou" style="display:none;">
<p>Thank you</p>
</div>
</div>
jquery的
<script type="text/javascript" src="http://www.example.com/javascript/jquery.tools.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#emailForm .validate').validator();
$('#emailForm #submit').submit(function (e) {
var form = $(this);
if (!e.isDefaultPrevented()) {
$.ajax({
type: "POST",
url: "http://www.example.com/components/handlers/FormRequestHandler.ashx/EmailFormRequestHandler",
data: JSON.stringify(form),
dataType: 'json',
cache: false,
success: function (msg) {
if (msg) {
$('emailForm-content').hide();
$('emailForm-thankyou').show();
}
},
error: function (msg) {
form.data("validator").invalidate(msg);
}
});
}
e.preventDefault();
});
});
</script>