开发者

asp .net mvc ajax submit

I am facing a problem that's driving my crazy for sometime. I have an asp .net mvc page(.ascx). This view has a form which can be ajax submitted to a controller action. I have an ajax submit button that posts the form.

In the view I have some if conditions as follows

 If(Model.Sampl开发者_如何学GoeID.HasValue) 
{
  
    alert('sample created');
    alert('');
  
}

This code works fine when I hit the ajax submit button, I mean the javascript gets executed. But when I hit the enter key, the same post happens but the javascript code is not executed at all. I don't know why this happens.

Any thoughts or comments?


Just a thought. Could be that your ajax submit button is only binded to the click event. If so u will have to bind it to the keypress event.Yes its better that you post the html.


put the following code in in the document.ready() function

$("input").keydown(function(e){
    if(e.which==13)
    {
        //Your Submit Method
    }
});


I'm going to differ from the others who have posted and say that you are binding your jQuery wrong to your form submission and you need to rework that. Do not bind to mouse click on the button and do not inspect keydowns.

Instead, forms have a submit event that can be bound to. It won't matter whether they click the submit button, press enter, or tab to the button and press the space bar. Bind thusly:

$('#yourformcontainer form').submit(function() {
     //Your custom submit code here
     //return false if you want to abort the form submission, true (or nothing) otherwise
}); 

This makes the assumption that your HTML form is rendered something like this:

<div id="yourformcontainer">
    <form>
        ...
        <input type="submit" value="submit" />
    </form>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜