2015-09-07 64 views
1

我有一個表單,應通過ajax提交時提交按鈕被點擊,但我得到函數未定義。表單在一個div中被加載到主頁面中,所以我已經把這些標籤放在了表單之下,以確保表單在腳本之前加載。閱讀了一些其他的解決方案,我嘗試添加window.NewSearchFunction而不是NewsearchFuntion,但它仍然無法正常工作。函數未定義按鈕單擊

我的形式:

<form class="inline" name="Form1"> 
    <input class="inline" name="employeeid" id="employeeid" type="hidden" size="30" autofocus value="<?php echo $employee[$k]["employeeid"]; ?>" /> 

    //More Fields Here 

    <p class="inline"> 
     <label class="inline" for="pensioncompany">Company Pension:</label> 
     <input class="inline" type="text" id="pensioncompany" name="pensioncompany" size="30" value="<?php echo $employee[$k]["pensioncompany"]; ?>" /> 
    </p> 
    <input id="submit" type="submit" onclick="NewSearchFunction(e)" value="Update Details"> 
    </td></tr></table></td></tr></table> 
</form> 
<script src="editemployeedetailsajax.js"></script> 

editemployeedetailsajax.js:

// JavaScript Document 
var NewSearchFunction; 
$(document).ready(function(){ 
    var window.NewSearchFunction; 

    NewSearchFunction(e) = function() { 
     e.preventDefault() 
     console.log() 
     var employeeid=document.getElementById("employeeid").value; 
     var firstname=document.getElementById("firstname").value; 
     var surname=document.getElementById("lastname").value; 
     var addr1=document.getElementById("addr1").value; 
     var addr2=document.getElementById("addr2").value; 
     var town=document.getElementById("town").value; 
     var county=document.getElementById("county").value; 
     var postcode=document.getElementById("postcode").value; 
     var tel1=document.getElementById("tel1").value; 
     var work=document.getElementById("work").value; 
     var mobile=document.getElementById("mobile").value; 
     var sex=document.getElementById("sex").value; 
     var DOB=document.getElementById("DOB").value; 
     var ninumber=document.getElementById("ninumber").value; 
     var payrollnumber=document.getElementById("payrollnumber").value; 
     var sortcode=document.getElementById("sortcode").value; 
     var accountnumber=document.getElementById("accountnumber").value; 
     var annualleaveentitlement=document.getElementById("annualleaveentitlement").value; 
     var vehicleid=document.getElementById("vehicleid").value; 
     var fuelcardid=document.getElementById("fuelcardid").value; 
     var mobileid=document.getElementById("mobileid").value; 
     var pensionindividual=document.getElementById("pensionindividual").value; 
     var pensioncompany=document.getElementById("pensioncompany").value; 

     // Returns successful data submission message when the entered information is stored in database. 

     var dataString = 'employeeid=' + employeeid + '&firstname=' + firstname + '&surname=' + lastname + '&addr1=' + addr1 + '&addr2=' + addr2 + '&town=' + town + '&county=' + county + '&postcode=' + postcode + '&tel1=' + tel1 + '&work=' + work + '&mobile=' + mobile + '&sex=' + sex + '&DOB=' + DOB + '&ninumber=' + ninumber + '&payrollnumber=' + payrollnumber + '&sortcode=' + sortcode + '&accountnumber=' + accountnumber + '&annualleaveentitlement=' + annualleaveentitlement + '&vehicleid=' + vehicleid + '&fuelcardid=' + fuelcardid + '&mobileid=' + mobileid + '&pensionindividual=' + pensionindividual + '&pensioncompany=' + pensioncompany; 

     // AJAX code to submit form. 
     $.ajax({ 
      type: "POST", 
      url: "submit.php", 
      data: dataString, 
      cache: false, 
      success: function(html) { 
       alert(html); 
      }); 
     ); 
+1

移動準備的'外的函數()'。或使用jQuery來處理事件,_better_? – Tushar

+0

這已經停止了未定義的問題,但現在應該提交的所有內容都將進入地址欄並刷新頁面,而不是發佈到submit.php –

+0

從'type = submit'更改爲'type = button' –

回答

0

像圖莎爾指出,您的NewSearchFunction(e)中定義的DOM被加載後,才,但加載DOM的時候,輸入元素可以引用NewSearchFunction,此時尚未定義它。

更好的方法是確實使用jquery元素結合:除了

$('input#submit').click(function(e) { 
e.preventDefault() 
    console.log() 
    var employeeid=document.getElementById("employeeid").value; 
    var firstname=document.getElementById("firstname").value; 
    var surname=document.getElementById("lastname").value; 
    var addr1=document.getElementById("addr1").value; 
    var addr2=document.getElementById("addr2").value; 
    var town=document.getElementById("town").value; 
    var county=document.getElementById("county").value; 
    var postcode=document.getElementById("postcode").value; 
    var tel1=document.getElementById("tel1").value; 
    var work=document.getElementById("work").value; 
    var mobile=document.getElementById("mobile").value; 
    var sex=document.getElementById("sex").value; 
    var DOB=document.getElementById("DOB").value; 
    var ninumber=document.getElementById("ninumber").value; 
    var payrollnumber=document.getElementById("payrollnumber").value; 
    var sortcode=document.getElementById("sortcode").value; 
    var accountnumber=document.getElementById("accountnumber").value; 
    var annualleaveentitlement=document.getElementById("annualleaveentitlement").value; 
    var vehicleid=document.getElementById("vehicleid").value; 
    var fuelcardid=document.getElementById("fuelcardid").value; 
    var mobileid=document.getElementById("mobileid").value; 
    var pensionindividual=document.getElementById("pensionindividual").value; 
    var pensioncompany=document.getElementById("pensioncompany").value; 

    // Returns successful data submission message when the entered information is stored in database. 

    var dataString = 'employeeid=' + employeeid + '&firstname=' + firstname + '&surname=' + lastname + '&addr1=' + addr1 + '&addr2=' + addr2 + '&town=' + town + '&county=' + county + '&postcode=' + postcode + '&tel1=' + tel1 + '&work=' + work + '&mobile=' + mobile + '&sex=' + sex + '&DOB=' + DOB + '&ninumber=' + ninumber + '&payrollnumber=' + payrollnumber + '&sortcode=' + sortcode + '&accountnumber=' + accountnumber + '&annualleaveentitlement=' + annualleaveentitlement + '&vehicleid=' + vehicleid + '&fuelcardid=' + fuelcardid + '&mobileid=' + mobileid + '&pensionindividual=' + pensionindividual + '&pensioncompany=' + pensioncompany; 

    // AJAX code to submit form. 
    $.ajax({ 
     type: "POST", 
     url: "submit.php", 
     data: dataString, 
     cache: false, 
     success: function(html) { 
      alert(html); 
     }); 
    ); 
}); 
0

放置onclick屬性上的按鈕或按鈕註冊一個點擊事件處理程序,你可以用type="submit"離開按鈕,然後在表單上註冊一個提交事件處理程序。您只需在submit-event處理程序中調用event.preventDefault()以防止正常的表單提交,當然,您應該在文檔就緒處理程序中註冊事件處理程序。

相關HTML:

<script src="editemployeedetailsajax.js"></script> 
<form name="Form1"> 
    <input type="hidden" name="employeeid" value="..." /> 
    <input type="text" name="pensioncompany" value="..." /> 
    <input type="submit" value="Update Details" /> 
</form> 

editemployeedetailsajax.js:

$(document).ready(function() { 
    $('form[name=Form1]').submit(function(event) { 
     event.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      url: "submit.php", 
      data: $(this).serialize(), 
      dataType: 'html', 
      cache: false, 
      success: function(html) { 
       alert(html); 
      } 
     }); 
    }); 
});