jQuery 提交功能不起作用(内部函数)
我对这个 jQuery 代码有疑问。它没有按预期工作:
$('#select_dropdown').change ( function(){
$('#form_to_submit').submit( function(event){
$.post("list.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
});
});
但是,这有效:
$('#select_dropdown').change ( function(){
$('#form_to_submit').submit();
});
我想知道为什么提交的内部函数不起作用。当用户从下拉列表中选择一个值时,必须提交表单。第二组代码可以工作,但是如果我添加要提交的内部函数,则不会。
基本上,我想在用户选择下拉菜单后进行一些ajax调用。
I have a problem with this jQuery code. It doesn't work as expected:
$('#select_dropdown').change ( function(){
$('#form_to_submit').submit( function(event){
$.post("list.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
});
});
However, this works:
$('#select_dropdown').change ( function(){
$('#form_to_submit').submit();
});
I wonder why the internal function on submit doesn't work. When a user selects a value from a dropdown, the form must be submitted. The second set of codes work but if I add an inner function to submit, it doesn't.
Basically, I want to do some ajax call after the user select on the dropdown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据文档( http://api.jquery.com/submit/ ),
提交不带参数的 ()
将提交表单,但如果包含参数,它将把submit
事件绑定到表单,但不会提交。因此,@Chris Fulstow 发布的代码将是提交表单的正确方法,但由于 ajax 不是同步的,函数将继续而不等待答案,然后将不会显示警报。
您可以使其同步,但必须使用
$.ajax
而不是$.post
,因为$.post
不包含 <代码>异步选项。无论如何,我正在为您的具体问题提供解决方案,但我想应该有更好的方法来做到这一点。According to documentation ( http://api.jquery.com/submit/ ),
submit()
without parameters will submit your form, but if you include arguments it will bind thesubmit
event to the form, but it wont submit it.So, the code posted by @Chris Fulstow would be the right way of submitting the form, but as ajax is not synchronous, function will continue without waiting for the answer and then, the alert will not be shown.
You can make it synchronous, but you must use
$.ajax
instead of$.post
, because$.post
doesn't include anasync
option. Anyway, I'm providing a solution for your specific problem, but I'm guess there should be a better way for doing it.当您使用回调参数
submit( handler(eventObject) )
进行调用时,它只会附加一个事件处理程序。要触发表单提交,请调用不带参数的submit()
:When you call with a callback argument
submit( handler(eventObject) )
it will only attach an event handler. To trigger a form submit, callsubmit()
with no arguments:第一个示例中的
.submit
调用将函数绑定到表单上的提交事件。来自精细手册:您需要
在其他地方绑定您的提交处理程序:并像第二个示例中那样调用
submit
。The
.submit
call in your first example is binding a function to the submit event on the form. From the fine manual:You need to bind your submit handler:
somewhere else and call
submit
as in your second example.所以这里的问题是,在第一种情况下,您将事件处理程序绑定到元素,而在第二种情况下,您将触发它。让我们看看第一种情况:
您本质上是在做类似的事情
第二种情况是您指示表单提交,这就是它起作用的原因。如果您希望处理程序使用您的自定义代码,您将需要它们。首先绑定您的事件处理程序,然后 onchange 指示表单提交。
但请记住,正如其他人已经说过的那样,如果您的操作设置为转到另一个位置,您可能看不到绑定事件处理程序的结果,因此您不必显式声明操作的 url,而是必须使用诸如
action="javascript:void(0)"
之类的东西来阻止表单进入任何地方。为了让你的代码更简洁,你可以将 ajax 从一个未命名的函数中取出,并将其放入一个命名的函数中,并在更改时调用它,这样它看起来像这样。
我还没有运行过这段代码,请原谅我犯的任何愚蠢的语法错误。但这应该能让你有所收获。祝你好运! :)
So the problem here is that in the first case, you are binding an event handler to the element, and in the second you are triggering it. Let's look at the first case:
You're essentially doing something like
The second case is you instructing the form to submit, which is why it works. If you wanted the handler to work with your custom code you would need both of them. First bind your event handler, then, onchange instruct the form to submit.
Keep in mind though, that as other people have already said, if your action is set to go to another location, you may not see the results of the binded event handler so instead of explicitly stating a url for your action, you will have to use something to prevent the form from going anywhere like
action="javascript:void(0)"
or the like.To make your code a bit cleaner, you could pull the ajax out of an unnamed function and put it in a named one and call it on change so it looks like this.
I haven't run this code, please excuse any silly syntax mistakes I've made. But this should get you some of the way there. Good luck! :)