How to raise "completed" event on a submit event?
The controller开发者_如何学运维 action is called after the completedCallback not after $(this).submit(). See the code below. How can i trigger the action before raising the completed callback without using ajax?
controller:
public ActionResult MyFunction(FormCollection data){
//this should trigger before calling the CompletedEvent
}
javascript
var flag = false;
$(form).submit(function(){
if(!flag){
//raise start event
startedCallback.call();
flag = true;
$(this).submit();
//raise completed event
completedCallback.call();
}
});
If you do not want to use ajax, you cannot reliably call something after the submit - the form's processing is done. You must use ajax if you want a response.
If you want to call your function BEFORE your form submit - again.. you MUST use some form of ajax.
See How do I capture response of form.submit
If you really need control of events like this - you need ajax or iframe hacks. Sorry : )
Maybe your action can return a url var, like ?status=submitted
?
Then check in your javascript if that var exists and if so, trigger your completedCallback
.
By the way, you should return false
in your $(form).submit(function(){return false;});
if you dont want the form to be submitted if flag == true
.
精彩评论