jQuery:表单不使用 $(“#id”).submit() 提交,但会使用“submit”提交按钮?
<form method="post" action="load_statements.php?action=load" id="loadForm"
enctype="multipart/form-data">
这就是我的形式,对我来说看起来不错。在该表单中,我放置了此按钮:
<input type="button" name="submit" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />
这是它调用的函数:
function confirmSubmit() {
// get the number of statements that are matched
$.post(
"load_statements.php?action=checkForReplace",
{
statementType : $("#statementType").val(),
year : $("#year").val()
},
function(data) {
if(data['alreadyExists']) {
if( confirm("Statements already exist for " + $("#statementType").html() + " " + $("#year").val() +
". Are you sure you want to load more statements with these settings? (Note: All duplicate files will be replaced)"
)) {
$("#loadForm").submit();
}
} else {
$("#loadForm").submit();
}
}, "json"
);
}
如您所见,它调用 $("#loadForm").submit();
,但表单未提交(即页面不刷新)。这是为什么?
谢谢!
<form method="post" action="load_statements.php?action=load" id="loadForm"
enctype="multipart/form-data">
that is my form, which looks fine to me. in that form, i put this button:
<input type="button" name="submit" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />
here is the function it calls:
function confirmSubmit() {
// get the number of statements that are matched
$.post(
"load_statements.php?action=checkForReplace",
{
statementType : $("#statementType").val(),
year : $("#year").val()
},
function(data) {
if(data['alreadyExists']) {
if( confirm("Statements already exist for " + $("#statementType").html() + " " + $("#year").val() +
". Are you sure you want to load more statements with these settings? (Note: All duplicate files will be replaced)"
)) {
$("#loadForm").submit();
}
} else {
$("#loadForm").submit();
}
}, "json"
);
}
and as you can see, it calls $("#loadForm").submit();
, but the form is not submitting (ie. the page is not refreshing). why is that?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
试试这个,如果您确实以 id 或名称提交,则对我有用
,您需要重命名该表单元素。
如果您想在脚本中使用 element.submit() ,则不能将 id="submit" 赋予表单的任何其他元素。
表单提交 jQuery 不起作用
Try this, worked for me
if you do have submit as id or name, you need to rename that form element.
can not give id="submit" to any other element of form if you want to use element.submit() in script.
Form Submit jQuery does not work
看起来您的提交发生在回调中,该回调仅在 onclick 处理程序中的 ajax post 之后运行。
换句话说,当您单击提交时,浏览器必须首先执行 checkForReplace 操作。
Looks like you have the submit happening in a callback that only runs after an ajax post in your onclick handler.
In other words, when you click submit, the browser has to first go out and hit the checkForReplace action.
它的 Ajax 调用,为什么您需要在已经使用 ajax 处理表单的情况下提交表单。您可以使用以下方式导航离开页面
its Ajax call, why you need to submit your form while you already process it using ajax. You can navigate away from page using
我遇到了类似的问题,花了一段时间才弄清楚。如果您不向 jQuery 的 .submit() 函数传递处理程序,则它应该触发表单提交,但它不会的原因是您的提交按钮名为“submit”并且覆盖了表单的提交函数。
即 jQuery 调用 DOM 对象的
不确定为什么控制台不记录错误,也许 jQuery 首先检查提交函数是否存在,并且在以下情况下忽略请求:该函数不存在。
I had a similar problem, took a while to figure it out. jQuery's .submit() function should trigger a form submit if you don't pass a handler to it, but the reason it doesn't is because your submit button is named "submit" and is overriding the submit function of the form.
i.e. jQuery calls the
<form>.submit()
function of the DOM object, but all inputs within a form can also be access by calling<form>.<inputname>
. So if one of your input's is named 'submit' it overrides the<form>.submit()
function with<form>.submit
- being the input DOM element.... if that makes sense? So when jQuery calls<form>.submit()
it doesn't work because submit is no longer a function. So, if you change the name of your button it should work.Not sure why the console doesn't log an error, perhaps jQuery is first checking that the submit function exists and is just ignoring the request if the function doesn't exist.
我在表单中添加了一个不可见的提交按钮,我没有调用submit()函数,而是调用提交按钮上的click()函数=)
按钮:
绕过表单提交的疯狂方法:
$("#alternateSubmit").click();
i added an invisible submit button to the form, and instead of calling the submit() function, i call the click() function on the submit button =)
the button:
<div style="display:none"><input id="alternateSubmit" type="submit" /></div>
the crazy way to get around the form submission:
$("#alternateSubmit").click();
删除提交按钮中的属性“名称”:
这是您的代码:
应该是这样的:
erase attribute "name" in your submit button :
this is your code :
should be like this :
将按钮的“提交”名称更改为其他名称。这导致了问题。
请参阅 dennisjq 的回答:
http://forum.jquery.com/topic/submiting-a -form-programmaticly-not-working
Change button's "submit" name to something else. That causes the problem.
See dennisjq's answer at:
http://forum.jquery.com/topic/submiting-a-form-programmatically-not-working
我在这里使用
$("form")[0].submit()
$("form")
返回一个 DOM 元素数组,因此通过访问相关索引我们可以获取表单元素的 DOM 对象并在该 DOM 元素中执行submit()
函数。缺点是如果一个 html 页面中有多个表单元素,则必须了解“表单”的正确顺序
I use
$("form")[0].submit()
here
$("form")
returns an array of DOM elements so by accessing the relevant index we can get the DOM object for the form element and execute thesubmit()
function within that DOM element.Draw back is if you have several form elements within one html page you have to have an idea about correct order of "form"s
http://api.jquery.com/submit/
jQuery
submit()< /code> event 添加一个事件侦听器,在您提交表单时发生。因此,您的代码基本上没有将任何内容绑定到提交事件。 如果您希望提交该表单,请使用老式 JavaScript
document.formName.submit()
。我将原封不动地保留在上面,以指出我的出发点。我的意思是说,如果您有这样的函数,那么为什么您会在 ajax 部分中发布值,然后使用 jQuery 提交表单,这会令人困惑。在这种情况下,我会将事件绑定到按钮的单击,然后如果您希望它返回,则返回 true,否则返回 false,如下所示:
如果此函数返回 true,则算作提交成功单击按钮并发生正常的浏览器行为。然后,您还以事件处理程序的形式将逻辑与标记分开。
希望这更有意义。
http://api.jquery.com/submit/
The jQuery
submit()
event adds an event listener to happen when you submit the form. So your code is binding essentially nothing to the submit event. If you want that form to be submitted, you use old-school JavaScriptdocument.formName.submit()
.I'm leaving my original answer above intact to point out where I was off. What I meant to say, is that if you have a function like this, it's confusing why you would post the values in an ajax portion and then use jQuery to submit the form. In this case, I would bind the event to the click of the button, and then return true if you want it to return, otherwise, return false, like so:
If this function returns true, then it counts as successful click of the submit button and the normal browser behavior happens. Then you've also separated the logic from the markup in the form of an event handler.
Hopefully this makes more sense.