jQuery 提交功能不起作用(内部函数)

发布于 2024-11-08 23:50:21 字数 544 浏览 0 评论 0原文

我对这个 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 技术交流群。

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

发布评论

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

评论(4

瀟灑尐姊 2024-11-15 23:50:21

根据文档( http://api.jquery.com/submit/ ),提交不带参数的 () 将提交表单,但如果包含参数,它将把 submit 事件绑定到表单,但不会提交。

因此,@Chris Fulstow 发布的代码将是提交表单的正确方法,但由于 ajax 不是同步的,函数将继续而不等待答案,然后将不会显示警报。

您可以使其同步,但必须使用 $.ajax 而不是 $.post,因为 $.post 不包含 <代码>异步选项。无论如何,我正在为您的具体问题提供解决方案,但我想应该有更好的方法来做到这一点。

$(function() {
    $('#select_dropdown').change(function() {
        $('#form_to_submit').submit();
    });

    $('#form_to_submit').submit(function(event) {
        $.ajax(
            url: "list.php",
            data: { name: "John", time: "2pm" },
            success: function(){
                alert("Data Loaded: " + data);
            },
            async:false,
        );
    });
});

According to documentation ( http://api.jquery.com/submit/ ), submit() without parameters will submit your form, but if you include arguments it will bind the submit 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 an async option. Anyway, I'm providing a solution for your specific problem, but I'm guess there should be a better way for doing it.

$(function() {
    $('#select_dropdown').change(function() {
        $('#form_to_submit').submit();
    });

    $('#form_to_submit').submit(function(event) {
        $.ajax(
            url: "list.php",
            data: { name: "John", time: "2pm" },
            success: function(){
                alert("Data Loaded: " + data);
            },
            async:false,
        );
    });
});
枫林﹌晚霞¤ 2024-11-15 23:50:21

当您使用回调参数 submit( handler(eventObject) ) 进行调用时,它只会附加一个事件处理程序。要触发表单提交,请调用不带参数的 submit()

$(function() {

    $('#select_dropdown').change(function() {
        $('#form_to_submit').submit();
    });

    $('#form_to_submit').submit(function(event) {
        $.post(
            "list.php",
            { name: "John", time: "2pm" },
            function(data) {
                alert("Data Loaded: " + data);
            }
        );
    });

});

When you call with a callback argument submit( handler(eventObject) ) it will only attach an event handler. To trigger a form submit, call submit() with no arguments:

$(function() {

    $('#select_dropdown').change(function() {
        $('#form_to_submit').submit();
    });

    $('#form_to_submit').submit(function(event) {
        $.post(
            "list.php",
            { name: "John", time: "2pm" },
            function(data) {
                alert("Data Loaded: " + data);
            }
        );
    });

});
滥情空心 2024-11-15 23:50:21

第一个示例中的 .submit 调用将函数绑定到表单上的提交事件。来自精细手册

.submit( handler(eventObject) )
handler(eventObject) 每次触发事件时执行的函数。

您需要

$('#form_to_submit').submit(function(event){ /*...*/ })

在其他地方绑定您的提交处理程序:并像第二个示例中那样调用 submit

The .submit call in your first example is binding a function to the submit event on the form. From the fine manual:

.submit( handler(eventObject) )
handler(eventObject) A function to execute each time the event is triggered.

You need to bind your submit handler:

$('#form_to_submit').submit(function(event){ /*...*/ })

somewhere else and call submit as in your second example.

旧城烟雨 2024-11-15 23:50:21

所以这里的问题是,在第一种情况下,您将事件处理程序绑定到元素,而在第二种情况下,您将触发它。让我们看看第一种情况:

$('#form_to_submit').submit(function(evt){ ... });

您本质上是在做类似的事情

document.getElementById('form_to_submit').addEventListener(
    'submit',
    function(evt){...},
    false
);

第二种情况是您指示表单提交,这就是它起作用的原因。如果您希望处理程序使用您的自定义代码,您将需要它们。首先绑定您的事件处理程序,然后 onchange 指示表单提交。

$('#form_to_submit').submit(function(evt){ ... });

$('#select_dropdown').change(function(){
    $('#form_to_submit').submit();
});

但请记住,正如其他人已经说过的那样,如果您的操作设置为转到另一个位置,您可能看不到绑定事件处理程序的结果,因此您不必显式声明操作的 url,而是必须使用诸如 action="javascript:void(0)" 之类的东西来阻止表单进入任何地方。

为了让你的代码更简洁,你可以将 ajax 从一个未命名的函数中取出,并将其放入一个命名的函数中,并在更改时调用它,这样它看起来像这样。

var fetchList = function(){
    $.ajax(...);
};

$('#form_to_submit').submit(fetchList);
$('#select_dropdown').change(fetchList);

我还没有运行过这段代码,请原谅我犯的任何愚蠢的语法错误。但这应该能让你有所收获。祝你好运! :)

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:

$('#form_to_submit').submit(function(evt){ ... });

You're essentially doing something like

document.getElementById('form_to_submit').addEventListener(
    'submit',
    function(evt){...},
    false
);

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.

$('#form_to_submit').submit(function(evt){ ... });

$('#select_dropdown').change(function(){
    $('#form_to_submit').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.

var fetchList = function(){
    $.ajax(...);
};

$('#form_to_submit').submit(fetchList);
$('#select_dropdown').change(fetchList);

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! :)

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