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

當我點擊提交按鈕時,此功能工作得很好。但是我想在按下按鈕時提交表單。上面的函數寫在邊上帶有文件上傳的jquery窗體 - onclick而不是dom準備好

$(document).ready(function() { 

但我想寫它在一個正常的JavaScript函數內。

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

+1

** ** form.min.js是模糊的 - 它可以是任何事情。爲什麼你不能把這個代碼放在一個「正常」函數中? – ahren

回答

1

好,然後訂閱你的DOM元素的單擊處理:

$(document).ready(function() { 
    $('#myButton').click(function() { 
     $("#form").ajaxSubmit(
      beforeSubmit: function(before) { 
       $('.result').html('loading'); 
      }, 
      success: function(d) { 
       //result process 
      } 
     );  
     return false; 
    }); 
}); 
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> 
0
<button id="formSubmit"> 

結合formsubmit一個按鈕點擊事件應該像這樣工作:

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

你幾乎可以得到它的一切,在document.ready內部綁定的關鍵在於dom已準備好被讀取,並且我們知道它安全地設置dom元素的事件處理程序,正常的實踐是在你內部docuement.ready處理程序分配綁定您選擇的元素,asuming你有蒙山「submitImageForm」的ID按鈕的代碼是lokking財產以後這樣

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

    }) 
    }) 
})