我有一個表單,應通過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);
});
);
移動準備的'外的函數()'。或使用jQuery來處理事件,_better_? – Tushar
這已經停止了未定義的問題,但現在應該提交的所有內容都將進入地址欄並刷新頁面,而不是發佈到submit.php –
從'type = submit'更改爲'type = button' –