我理解這個問題可能是一樣的。我用Google搜索了一下,不幸的是我可能錯過了類似的東西。我對此表示歉意。無法打電話給jquery ajax
我已經在js基於一些json響應創建了一個動態表並相應地更新了表。現在我有一個提交按鈕,將當前選定的原始數據作爲ajax調用發佈。但我無法致電jquery。 以下是代碼:
JS:
$(document).ready(function(){
$('#ajax-button').click(function() {
var formData=$("#careers").serialize();
console.log("This is the skills : "+formData);
$.ajax({
type : "POST",
url : "currentopening",
//data : "skills="+skills,
data:formData,
dataType : "json",
async: true,
success : function(response) {
var obj = JSON.parse(response);
console.log("length of json response : "+obj.length)
if(obj.length>0){
//for (var i = 0; i < obj.length; i++)
//{
console.log("the characteristics : "+obj[0].title);
addRow(obj);
//}
}
else{
document.getElementById("dynamictable").innerHTML="No jobs found!";
}
}
});
});
});function addRow(obj) {
var table_1 = document.getElementById("dynamictable");
for(var i=0;i<obj.length;i++){
var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
btn.id="apply-button";
var input = document.createElement("input");
input.setAttribute("id",obj[i].jobID);
input.setAttribute("type", "hidden");
input.setAttribute("name", "jobID");
input.setAttribute("value", obj[i].jobID);
var rowCount = table_1.getElementsByTagName("tr").length;
var row1 = table_1.insertRow(rowCount);
row1.id="x";
var cell0=row1.insertCell(0);
var cell1=row1.insertCell(1);
var cell2=row1.insertCell(2);
var cell3=row1.insertCell(3);
var cell4=row1.insertCell(4);
var cell5=row1.insertCell(5);
var cell6=row1.insertCell(6);
var cell7=row1.insertCell(7);
var cell8=row1.insertCell(8);
var cell9=row1.insertCell(9);
cell0.headers="title"; cell1.headers="qualification"; cell2.headers="salary"; cell3.headers="companyName"; cell4.headers="tags"; cell5.headers="location";
cell6.headers="experience"; cell7.headers="aboutCompany";cell8.id="hidden-element";
cell9.id="doit";
cell0.innerHTML= obj[i].title;
cell1.innerHTML= obj[i].qualification;
cell2.innerHTML= obj[i].salary;
cell3.innerHTML= obj[i].companyName;
cell4.innerHTML= obj[i].tags;
cell5.innerHTML= obj[i].location;
cell6.innerHTML= obj[i].experience;
cell7.innerHTML= obj[i].aboutCompany;
document.getElementById("doit").appendChild(btn);
document.getElementById("hidden-element").appendChild(input);}};$('#apply-button').click(function() {
console.log("id of current element : ");
var formData=document.getElementById(this);
console.log("id of current element : "+formData);
//console.log("This is the filter : "+formData);
$.ajax({
type : "GET",
url : "apply",
//data : "skills="+skills,
data:"jobID="+formData,
dataType : "text",
async: true,
success : function(response) {console.log("The response : "+response);
}
});
});
我懷疑,因爲ID 應用按鈕是動態創建的,它是不是在document.load功能,我的按鈕不能夠工作。請讓我知道是否需要更多信息。
我希望對此有所迴應。
你getiing錯誤?如果是這樣,請張貼在這裏 – Derek
嗨,奧利弗,沒有錯誤,頁面重新加載。 – coolmego
請在這裏發佈整個文件 – Derek