我似乎无法弄清楚如何使用Ajax从jquery ui按钮函数提交表单。
这是我的脚本,通过传统的方式提交表单
$('#contact-form').dialog({
modal: true,
autoOpen: false,
buttons: {
Cancel: function () {
$(this).dialog('close');
},
Ok: function () {
$('form#contactUs').submit();
$(this).dialog('destroy');
}
}
});
$('#contact-us').click(function () {
$('#contact-form').dialog('open');
return false;
});
});
这是我的表格
<div id="contact-form" class="hidden" title="Online Request Form">
<form action="/Main/Contact/contactUs" method="post">
<div>
<label for="Name">Name</label><br />
<input name="Name" /><br />
<label for="PhoneNumber">Phone Number</label><br />
<input name="PhoneNumber" /><br />
<label for="EmailAddress">Email Address</label><br />
<input name="EmailAddress" /><br />
<label for="Question">Question or Comment</label><br />
<textarea name="Question"></textarea><br />
<label for="Security">What color is an orange?</label><br />
<input name="Security" />
<noscript>
<input type="submit" name="submit" value="Ok" />
</noscript>
</div>
</form>
</div>
如何更改此设置以通过Ajax提交链接以便我可以显示成功消息?
答案 0 :(得分:8)
这可能就足够了:
$("#contact-form form").submit(function(e) {
e.preventDefault();
$.post( $(this).attr('action'), $(this).serialize(),
function(resp) {
if(resp == "success") {
alert('Submission was successful');
} else {
// something else
}
}
});
});
简要说明:
进一步阅读:
答案 1 :(得分:0)
这是我找到的解决方案
<div id="contact-form" class="hidden" title="Online Request Form">
@Using (Ajax.BeginForm("Contact", "Main",
Nothing,
New AjaxOptions With {.UpdateTargetId = "", .HttpMethod = "post"},
New With {.id = "contactUs"}))
@<div>
<label for="Name">Name</label><br />
<input name="Name" /><br />
<label for="Phone">Phone Number</label><br />
<input name="Phone" /><br />
<label for="Email">Email Address</label><br />
<input name="Email" /><br />
<label for="Question">Question or Comment</label><br />
<textarea name="Question"></textarea><br />
<label for="Security">What color is an orange?</label><br />
<input name="Security" />
<noscript>
<input type="submit" name="submit" value="Ok" />
</noscript>
</div>
End Using
</div>
<script>
$(function () {
$('#contact-form').dialog({
modal: true,
autoOpen: false,
buttons: {
Cancel: function () {
$(this).dialog('close');
},
Ok: function () {
$('form#contactUs').trigger('submit');
$(this).submit();
}
}
});
$('#contact-us').click(function () {
$('#contact-form').dialog('open');
return false;
});
});
</script>