我有输入电子邮件和消息文本时向我发送消息的表单。我想同时使用 jQuery Validation Engine 插件和 ajax 响应来返回结果。为此,我jQuery('#messageSendButton').click()
在每种情况下都使用相同的功能。
从控制器返回响应:
<script type="text/javascript">
jQuery('#messageSendButton').click(function () {
var url = '@Url.Action("SendQuickMessage", "Home")';
var email = $('#email').val();
var messageText = $('#messageText').val();
$.ajax({
url: url,
type: 'GET',
contentType: 'application/json',
data: { Email: email, Message: messageText },
success: function (result) {
if (result.success) {
alert("Message sent! Thanks!");
}
else {
alert("Error occured!");
}
},
complete: function () {
$(".loading").hide();
}
});
});
</script>
并且,要验证表单:
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#quickMessageForm').validationEngine('init');
jQuery('#messageSendButton').click(function () {
jQuery('#quickMessageForm .loading').animate({ opacity: 1 }, 250);
if (jQuery('#quickMessageForm').validationEngine('validate')) {
jQuery.post('/Home/SendQuickMessage', {
Email: jQuery('#email').val(),
Message: jQuery('#message').val(),
formname: 'messageForm',
formtype: 'messageF'
}, function () {
jQuery('#quickMessageForm .loading').hide();
});
return false;
} else {
jQuery('#quickMessageForm .loading').animate({ opacity: 0 }, 250);
return false;
}
});
});
</script>
分别,它们都单独工作,但一起工作,首先 ajax 函数工作,第二个验证工作。这是不对的。. 我怎样才能将它们合二为一?首先验证表单,然后获取 ajax 响应。
编辑:
这是表格:
<form action="#" method="post" id="quickMessageForm">
<div class="form_info cmsms_input">
<label>Email<span class="color_3"> *</span></label>
@Html.TextBoxFor(model => model.Email, new { name = "email", id = "email", tabidex = 1, @class = "validate[required,custom[email]]" })
</div>
<div class="form_info cmsms_textarea">
<label>Message<span class="color_3"> *</span></label>
@Html.TextAreaFor(model => model.Message, new { id = "messageText", name = "messageText", tabindex = 2, @class = "validate[required,minSize[10]]" })
</div>
<div class="loading"></div>
<div>
<input type="submit" name="messageSendButton" id="messageSendButton" value="Send" class="button_small" tabindex="3" />
</div>
</form>