简体   繁体   English

asp .net mvc ajax提交

[英]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). 我有一个ASP .NET MVC页面(.ascx)。 This view has a form which can be ajax submitted to a controller action. 该视图具有可以ajax提交给控制器动作的形式。 I have an ajax submit button that posts the form. 我有一个发布表格的ajax提交按钮。

In the view I have some if conditions as follows 我认为我有一些以下条件

If(Model.SampleID.HasValue) 
{
  
    alert('sample created');
    alert('');
  
}

This code works fine when I hit the ajax submit button, I mean the javascript gets executed. 当我按下ajax提交按钮时,此代码工作正常,这意味着javascript被执行。 But when I hit the enter key, the same post happens but the javascript code is not executed at all. 但是,当我按下Enter键时,发生了同样的事情,但是javascript代码根本没有执行。 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. 可能是您的Ajax提交按钮仅绑定到click事件。 If so u will have to bind it to the keypress event.Yes its better that you post the html. 如果是这样,您将必须将其绑定到keypress事件。是的,最好发布html。

put the following code in in the document.ready() function 将以下代码放入document.ready()函数中

$("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. 我将与其他已发布的人有所不同,他们说您将jQuery与表单提交的绑定错误,需要对其进行重新处理。 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. 而是,表单具有可以绑定的submit事件。 It won't matter whether they click the submit button, press enter, or tab to the button and press the space bar. 他们是否单击提交按钮,按Enter或按Tab到按钮并按空格键都没有关系。 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: 假设您的HTML表单呈现如下形式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM