Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有文件上传的Jquery表单-onclick而不是dom ready_Javascript_Jquery - Fatal编程技术网

Javascript 带有文件上传的Jquery表单-onclick而不是dom ready

Javascript 带有文件上传的Jquery表单-onclick而不是dom ready,javascript,jquery,Javascript,Jquery,当我点击submit按钮时,这个功能运行得非常好。但我想在按下按钮时提交表格。上面的函数写在第三方 $("#form").submit(function() { $(this).ajaxSubmit({ beforeSubmit: function(before) { $('.result').html('loading'); }, success: function(d) {

当我点击submit按钮时,这个功能运行得非常好。但我想在按下按钮时提交表格。上面的函数写在第三方

 $("#form").submit(function() {
     $(this).ajaxSubmit({
         beforeSubmit: function(before) {
             $('.result').html('loading');
         },
         success: function(d) {
             //result process
         }
     }); 
     return false;
 });
但是我想在一个普通的javascript函数中编写它


我正在使用表单插件form.min.js

那么,订阅DOM元素的点击处理程序:

 $(document).ready(function() {
试试这个

$(document).ready(function() {
    $('#myButton').click(function() {
        $("#form").ajaxSubmit(
            beforeSubmit: function(before) {
                $('.result').html('loading');
            },
            success: function(d) {
                //result process
            }
        );    
        return false;
    });
});

函数uploadimg(){
$(此).ajaxSubmit({
提交前:函数(提交前){
$('.result').html('loading');
},
成功:功能(d){
//结果过程
}
});
返回false;
} 

您几乎已经了解了所有内容,在document.ready中绑定的要点是dom已经准备好读取,我们知道为dom元素设置事件处理程序是安全的,正常的做法是在您的docuement.ready处理程序中为您的元素分配绑定,前提是您有一个ID为的按钮“submitImageForm”代码可能是这样的

$('#formSubmit').on('click', function(){
    $('#form').ajaxSubmit({
         beforeSubmit: function(before) {
             $('.result').html('loading');
         },
         success: function(d) {
             //result process
         }
     }); 
     return false;
});
js是模棱两可的-它可以是任何东西。为什么不能将代码放在“普通”函数中?
<button id="formSubmit">
$('#formSubmit').on('click', function(){
    $('#form').ajaxSubmit({
         beforeSubmit: function(before) {
             $('.result').html('loading');
         },
         success: function(d) {
             //result process
         }
     }); 
     return false;
});
$(function(){
  $("#submitImageForm").click(function(e){ // tell the browser we wanner handle the onClick event of this element
    e.preventDefault() // this is to tell the browser that we are going to handle things and it shod not do its default (e.g sending the form up to the server and reloading)

    $("#form").submit(function() {
      $(this).ajaxSubmit({
         beforeSubmit: function(before) {
             $('.result').html('loading');
         },
         success: function(d) {
         //result process
         }
     })

    })
  })
})