我有一个表单,我使用 Ajax.BeginForm
提交该表单。我想做以下事情:
- 提交包含所有数据的表单(使用
Ajax.BeginForm
) - 在等待 Controller 时,做一些工作 - 包括截断我的文本区域
- 当 Controller 完成后(使用
OnSuccess
),执行更多操作
但是,虽然 1) 和 3) 工作完美,但我不知道通过哪种方式访问#2。我尝试过在表单上使用 JavaScript 事件 submit
,但此事件似乎以前发生过,因此如果我清空文本区域, Controller 永远不会看到此填充值。
我的 Razor :
@using (Ajax.BeginForm("CreateNotification", "Task",null, new AjaxOptions() {HttpMethod = "POST", OnSuccess= "newMessage" },new {id="conv_form"}))
{
@Html.HiddenFor(c => c.ConversationId)
<div class="form-group">
@Html.TextAreaFor(c => c.New_Message, new {@class = "form-control conversation-box"})
</div>
<input type="submit" value="Send message" id="conversation_message_submit" class="btn btn-link pull-right" style="color: black; padding-right: 0px; font-size: 12pt;"/>
}
我也有我的 jQuery:
<script>
$(document).ready(function() {
/// Here I plan to truncate my textarea + other stuff
$('#conv_form').submit(function () {
console.log('conv_form submit');
});
});
function newMessage(data) {
console.log('newmessage fired');
}
</script>
所以我的挑战是,通过我的 submit
事件,如果我操作字段,这将改变我们发送到 Controller 的内容。我想访问发送到 Controller 的事件和 OnSuccess
之间的事件。
最佳答案
您可以使用 ajaxStart
执行第 2 步,如下所示:
$(document).ajaxStart(function() {
// do step 2
});
ajaxStart()
方法指定 AJAX 请求启动时要运行的函数。从 jQuery 版本 1.9
开始,此方法只能附加到 document
。更多信息请参见this .
关于jquery - 如何获取 Ajax.BeginForm 和 OnSuccess 之间的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46015238/