jQuery:表单不使用 $(“#id”).submit() 提交,但会使用“submit”提交按钮?

发布于 2024-09-11 01:07:15 字数 1248 浏览 6 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

谁把谁当真 2024-09-18 01:07:18

试试这个,如果您确实以 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

动听の歌 2024-09-18 01:07:18

看起来您的提交发生在回调中,该回调仅在 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.

老旧海报 2024-09-18 01:07:18

它的 Ajax 调用,为什么您需要在已经使用 ajax 处理表单的情况下提交表单。您可以使用以下方式导航离开页面

window.location.href = "/somewhere/else";

its Ajax call, why you need to submit your form while you already process it using ajax. You can navigate away from page using

window.location.href = "/somewhere/else";
可是我不能没有你 2024-09-18 01:07:17

我遇到了类似的问题,花了一段时间才弄清楚。如果您不向 jQuery 的 .submit() 函数传递处理程序,则它应该触发表单提交,但它不会的原因是您的提交按钮名为“submit”并且覆盖了表单的提交函数。

即 jQuery 调用 DOM 对象的

.submit() 函数,但表单内的所有输入也可以通过调用 .< 来访问/代码>。因此,如果您的输入之一被命名为“submit”,它将使用 .submit 覆盖 .submit() 函数 - 作为输入 DOM 元素....如果这有道理吗?因此,当 jQuery 调用 .submit() 时,它不起作用,因为 Submit 不再是一个函数。 所以,如果你更改按钮的名称,它应该可以工作。

不确定为什么控制台不记录错误,也许 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.

桜花祭 2024-09-18 01:07:17

我在表单中添加了一个不可见的提交按钮,我没有调用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();

相权↑美人 2024-09-18 01:07:17

删除提交按钮中的属性“名称”:

这是您的代码:

<input type="button" name="submit" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />

应该是这样的:

<input type="button" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />

erase attribute "name" in your submit button :

this is your code :

<input type="button" name="submit" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />

should be like this :

<input type="button" id="submit" value="Submit" onclick="confirmSubmit()" class="smallGreenButton" />
诗酒趁年少 2024-09-18 01:07:16

将按钮的“提交”名称更改为其他名称。这导致了问题。
请参阅 dennisjq 的回答:
http://forum.jquery.com/topic/submiting-a -form-programmaticly-not-working

请参阅 jQuery Submit() 文档:

表单及其子元素不应使用以下输入名称或 ID
与表单的属性冲突,例如提交、长度或方法。
名称冲突可能会导致混乱的故障。如需完整列表
规则并检查您的标记是否存在这些问题,请参阅 DOMLint。

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

See the jQuery submit() documentation:

Forms and their child elements should not use input names or ids that
conflict with properties of a form, such as submit, length, or method.
Name conflicts can cause confusing failures. For a complete list of
rules and to check your markup for these problems, see DOMLint.

晨曦÷微暖 2024-09-18 01:07:16

我在这里使用 $("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 the submit() 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

浴红衣 2024-09-18 01:07:16

http://api.jquery.com/submit/

jQuery submit()< /code> event 添加一个事件侦听器,在您提交表单时发生。因此,您的代码基本上没有将任何内容绑定到提交事件。 如果您希望提交该表单,请使用老式 JavaScript document.formName.submit()


我将原封不动地保留在上面,以指出我的出发点。我的意思是说,如果您有这样的函数,那么为什么您会在 ajax 部分中发布值,然后使用 jQuery 提交表单,这会令人困惑。在这种情况下,我会将事件绑定到按钮的单击,然后如果您希望它返回,则返回 true,否则返回 false,如下所示:

$('#submit').click( function() {
  // ajax logic to test for what you want
  if (ajaxtrue) { return confirm(whatever); } else { return true;}
});

如果此函数返回 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 JavaScript document.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:

$('#submit').click( function() {
  // ajax logic to test for what you want
  if (ajaxtrue) { return confirm(whatever); } else { return true;}
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文