javascript - 将 jQuery 更改函数绑定(bind)到提交函数

标签 javascript jquery event-handling jquery-validate jquery-forms-plugin

我无法在更改输入后触发表单提交。我想我需要事件处理,但我从来没有真正了解那是什么。

这是我的 HTML:

<form action="/upload">
  <input type="file" name="file">
</form>

这是我的 JS:

$('input').change(function(e){
var closestForm=$(this).closest("form");
    closestForm.submit(function(e) {   
  closestForm.ajaxSubmit({   
              success: function(data){
                    console.log('form submitted successfully');
              }
      }); //ajaxSubmit
    return false;
  }); //submit 
}); //change

请注意,我想使用 AJAX 进行提交,但这不是问题,因为我的代码库的其他部分可以使用 jQuery Form plugin ajaxSubmit() 没问题。

想法?

最佳答案

因为您希望在输入更改时提交表单。你不需要在表单上提交事件

 $('input').change(function(e){
    var closestForm=$(this).closest("form"); 
      closestForm.ajaxSubmit({   
                  success: function(data){
                        console.log('form submitted successfully');
                  }
          }); //ajaxSubmit
        return false; 
    }); //change

关于javascript - 将 jQuery 更改函数绑定(bind)到提交函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629587/

相关文章:

javascript - 使用 on() 将事件处理程序添加到尚不存在的元素?

javascript - 将hintText替换为预定义列表

Java - Action 监听器 : Get components from parent

javascript - 触摸移动卡住忽略取消触摸移动的尝试

c# - 我应该在 Page_Init 中以编程方式添加控制事件处理程序吗?

javascript - 在 React Native 中从图像中删除背景

Javascript/Angular : Bind function to every object or make helper object?

javascript - 如何更新数据库中的所有行并在不刷新的情况下显示更新的数据?

javascript - HighCharts - 时间序列图表 - x 轴上不规则的日期时间间隔

Javascript:在for循环中隐藏原型(prototype)方法?