2013-07-31 28 views
0
$("#GetLog").submit(function (e) { 
     e.preventDefault(); 
     $.ajax({ 
       //some code 

     }); 
    }); 
    <form id="GetLog"> 
     <input type="submit"> 
    </form> 

我想在頁面加載時以及用戶按下提交按鈕時調用此函數。在頁面加載和表單提交時調用相同的函數

我曾嘗試在頁面加載document.getElementById("GetLog").submit()但它調用的函數。

+1

你真的應該使這個問題更容易閱讀。 – Hego555

+0

我認爲這很好。你的具體抱怨是什麼?這不是文獻,但它是可讀的。 –

回答

2

嘗試定義一個單獨的功能和負載通話,並提交

function ajaxCall(){ 
    $.ajax({ 
      //some code 

    }); 
} 

$(document).ready(function(){ 
    ajaxCall(); 
}); 

$("#GetLog").submit(function (e) { 
    e.preventDefault(); 
    ajaxCall(); 
}); 

希望這有助於

2

嘗試使用非匿名函數,只是傳遞函數的各種聽衆。

例子:

function submitFtn = function (e) { 
    e.preventDefault(); 
    $.ajax({ 
      //some code 

    } 
$("#GetLog").submit(submitFtn); 
$(document).ready(submitFtn) 

<form id="GetLog"> 
     <input type="submit"> 
</form> 
0

你可以試試這個 -

$(function(){ 
$("#GetLog").submit(function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      ... 
     }) 
}).submit(); 
}); 
1
$(document).ready(function(){ 
    $("#GetLog").submit(function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      //some code 

     }); 
    }); 
    //I want to call this function when page loads 
    $('#GetLog').trigger('submit'); 

}); 
+0

在頁面加載時提交表單是一種不好的做法:) – user2137186

2
window.onload = function(){ 
    //call the function 
} 

$(document).ready(function(){ 
    $(document).on('click','#GetLog',function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      //some code 

     }); 
    }); 
}) 
<form id="GetLog"> 
    <input type="submit"> 
</form> 
相關問題