jQuery 内部提交功能不起作用

发布于 2024-11-08 15:12:40 字数 525 浏览 2 评论 0原文

我对这个 jQuery 代码有疑问。它没有按预期工作:

$('#select_dropdown').change ( function(){
    $('#form_to_submit').submit( function(event){
        $.post("process.php", { name: "John" },
    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("process.php", { name: "John" },
    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技术交流群

发布评论

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

评论(2

夜唯美灬不弃 2024-11-15 15:12:40

第一个代码是为选择器 $('#list_count') 上的提交事件创建一个事件处理程序,很可能是一个表单元素。第二个代码是调用该元素的提交方法。当您使用任何方法提交表单时都会触发第一个,但它本身不会提交表单。你想做什么?

编辑

你想这样做吗

$('#select_dropdown').change ( function(){
    $.post("process.php", { name: "John" }, function(data) {
        alert("Data Loaded: " + data);
    });
});

The first code is creating an event handler for submit event on selector $('#list_count') most likely a form element. The second code is calling the submit method on that element. The first one would be triggered when you submit the form using any method but it itself would not submit the form. What are you trying to do?

EDIT

Do you want to do this

$('#select_dropdown').change ( function(){
    $.post("process.php", { name: "John" }, function(data) {
        alert("Data Loaded: " + data);
    });
});
风吹雨成花 2024-11-15 15:12:40

您可以执行以下操作:

$("#form_to_submit").submit(function(){
   //do your post stuff here
});

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

我正在做的是:

  1. 定义 form_to_submit 提交事件的处理程序
  2. 定义用户在 select_dropdown 上进行选择时的处理程序,这将调用提交即使在表格上。

=================

如果您有代码

$('#list_per_page').change ( function(){
    $('#list_count').submit( function(event){
        alert('It Works!');
    });
});

   $('#list_count').submit();

那么它就可以工作。原因是要出现“It Works”,您需要触发提交事件。

第一个块是在更改事件发生时向提交事件添加处理程序。因此,当 #list_per_page 更改时,提交事件处理程序将添加到 #list_count 元素中。

第二个块只是在 #list_count 上触发提交;因此,调用您在第一个块中添加的处理程序。

请记住,事件是异步发生的,看起来您希望代码按顺序运行。

我上面的代码不能解决您的问题;它仅用于演示目的。请告诉我们您想要实现什么,然后人们可以建议一些最佳实践来帮助您:-)

Here is what you can do:

$("#form_to_submit").submit(function(){
   //do your post stuff here
});

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

What I am doing is:

  1. Define the handler for submit event of form_to_submit
  2. Define the hander when user do the selection on select_dropdown, which invokes an submit even on the form.

=================

if you have the code

$('#list_per_page').change ( function(){
    $('#list_count').submit( function(event){
        alert('It Works!');
    });
});

and

   $('#list_count').submit();

then it will work. Reason being that for the "It Works" to appear, you need to trigger a submit event.

The first block is adding a handler to the submit event when the change event happens. So when #list_per_page changes, a submit event handler will be added to #list_count element.

The second block is just firing a submit even on #list_count; therefore, invoking the handler you added in the fist block.

Keep in mind that the evens happen asynchronously, it seems like you are expecting your code to run sequentially.

My code above is NOT a solution to you're problem; it is for demonstration purpose only. Please tell us what you want to achieve then people can suggest some best practices to help you :-)

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