使用 Jquery 和 mvc 提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15944559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:04:55  来源:igfitidea点击:

Form Submit using Jquery and mvc

jqueryasp.net-mvc

提问by user2067567

Am trying to submit a form using jQuery. My code below

我正在尝试使用 jQuery 提交表单。我的代码如下

<form id="sampleForm" action="@Url.Action("submitSearch","Home")">
    //Form Fields  textbox
    <button id="test" type="submit" ></button>
</form>

Inside document.readyhaving script

里面document.ready有脚本

$("#sampleForm").submit(function () {
    console.log("Success");
});

But when click sumbit nothing is happening.

但是当点击 sumbit 时什么也没有发生。

I tried the below code to see if I have mapped correctly and that works fine

我尝试了下面的代码,看看我是否已正确映射并且工作正常

$(document).on("click", "#search-button", function () {
    console.log("Success");
});

What am missing while submitting form ?

提交表单时遗漏了什么?

回答by Deepu

$("#test").click(function(){
   $("#sampleForm").submit();
});

回答by Zach

Is it possible you're submitting but the page is reloading and the console log is cleared?

是否有可能您正在提交但页面正在重新加载并且控制台日志已清除?

Also, just to clarify...

另外,只是为了澄清...

$("#sampleForm").submit(function () {
    console.log("Success");
});

This wires up an event to do stuff when the sampleForm is submitted...

当提交 sampleForm 时,这会连接一个事件来做一些事情......

$('#sampleForm').submit();

actually triggers the submit event

实际上触发了提交事件

回答by Zach

It should be like this

应该是这样的

<input id="test" type="submit" ></input>

Instead of this

而不是这个

<button id="test" type="submit" ></button>

回答by andres descalzo

This is the syntax for tag button:

这是 tag 的语法button

<button type="submit" value="Submit">Submit</button>

if you don't like or don't need put the text of button, and you only need run the event submit of a form, you cant do this:

如果您不喜欢或不需要放置按钮的文本,而您只需要运行表单的事件提交,则不能这样做:

html:

html:

<form id="sampleForm" action="@Url.Action("submitSearch","Home")" method="get">
    //Form Fields  textbox
</form>

js:

js:

// where needed
$('#sampleForm').submit();
// or
$('#sampleForm').trigger('submit');

but always you need put any tag input for compatibility with IE

但总是需要输入任何标签输入以与 IE 兼容