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

当我单击提交按钮时,此功能效果很好。但是我想在按下按钮时提交表单。上面的函数写在side

 $(document).ready(function() {

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

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

4

4 回答 4

1

好吧,然后订阅你的 DOM 元素的点击处理程序:

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

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

$('#formSubmit').on('click', function(){
    $('#form').ajaxSubmit({
         beforeSubmit: function(before) {
             $('.result').html('loading');
         },
         success: function(d) {
             //result process
         }
     }); 
     return false;
});
于 2012-10-19T07:41:48.080 回答
0

你几乎明白了,在 document.ready 中绑定的点就是准备读取 dom 的点,我们知道为 dom 元素设置事件处理程序是安全的,通常的做法是在你的 document.ready 中处理程序,您将绑定分配给您的元素,假设您有一个 ID 为“submitImageForm”的按钮,代码就像这样

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

    })
  })
})
于 2012-10-19T07:51:04.390 回答