使用Jquery和mvc表单提交

时间:2020-12-21 18:55:26

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.ready having script

内部文件。已经有脚本

$("#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 ?

提交表格时遗漏了什么?

4 个解决方案

#1


9  

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

#2


4  

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

实际上触发了提交事件

#3


1  

It should be like this

它应该是这样的

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

Instead of this

而不是这个

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

#4


0  

This is the syntax for 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:

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

js:

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

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

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

#1


9  

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

#2


4  

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

实际上触发了提交事件

#3


1  

It should be like this

它应该是这样的

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

Instead of this

而不是这个

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

#4


0  

This is the syntax for 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:

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

js:

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

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

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