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

时间:2012-10-19 07:37:27

标签: javascript jquery

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

当我点击提交按钮时,此功能非常有效。但是我想在按下按钮时提交表单。上面的函数写在

 $(document).ready(function() {

但是我想把它写在普通的javascript函数中。

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

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

试试这个

<form action='' method='post' onsubmit='return uploadimg();'>

<script>
function uploadimg(){
    $(this).ajaxSubmit({
     beforeSubmit: function(before) {
         $('.result').html('loading');
     },
     success: function(d) {
         //result process
     }
 });
 return false;
} 

</script>

答案 2 :(得分:0)

<button id="formSubmit">

将formsubmit绑定到按钮单击事件应该像这样工作:

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

答案 3 :(得分:0)

你几乎得到了所有,在文档中绑定的点。已经是dom准备好被读取的点,我们知道将dom元素设置为事件处理程序是安全的,正常的实践是你内心的docuement.ready处理程序你为你的元素分配绑定,假设你有一个带有“submitImageForm”ID的按钮代码就像这样的东西

$(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
         }
     })

    })
  })
})