我理解这个问题可能是一样的。我用google搜索,不幸的是我可能错过了类似的东西。我为此道歉。
我已经基于一些JSON响应在JavaScript中创建了一个动态表,并相应地更新了表。现在J有一个提交按钮,它将当前选择的原始数据作为ajax调用发布。在表中,每行的一个单元格中有一个隐藏元素,每行有一个Apply按钮。
所以,我想在按下按钮时获取隐藏的元素数据。
以下是代码:
** JS **
var cell0;var cell1;var cell2;var cell3;var cell4;var cell5;
var cell6;var cell7;var cell8;
var cell9;
var jobID;$(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";
cell0=row1.insertCell(0);
cell1=row1.insertCell(1);
cell2=row1.insertCell(2);
cell3=row1.insertCell(3);
cell4=row1.insertCell(4);
cell5=row1.insertCell(5);
cell6=row1.insertCell(6);
cell7=row1.insertCell(7);
cell8=row1.insertCell(8);
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);
}};$(document).on('click','#apply-button',function(event){
console.log("id of current element : ");
var formData=document.getElementById("dynamictable"); /*This is where i need to fetch the data*/
console.log("id of current element : "+formData);
//console.log("This is the filter : "+formData);
event.preventDefault();
$.ajax({
type : "GET",
url : "apply",
//data : "skills="+skills,
data:"jobID="+formData,
dataType : "text",
async: true,
success : function(response) {
console.log("The response : "+response);
}
});
});
当我点击应用按钮 ID时,我需要访问输入变量的 id 。
答案 0 :(得分:1)
我遇到了同样的情况,我最终使用的是dataTables。
它非常易于使用,并且有大量文档。
如果你正在使用它,你可以将表格中的数据转换为json对象并操纵数据。
我会将此作为评论添加,但我没有足够的声誉这样做。
希望这有帮助。
答案 1 :(得分:1)
我使用数据表来访问单元格列。使用api相对来说非常简单。这是代码:
** JS:**
function createTable(obj){
var myTableDiv = document.getElementById("populate_table");
var table = document.createElement('TABLE');
table.id='dynamictable';
var header = table.createTHead();
var row = header.insertRow(0);
var cell = row.insertCell(0);
cell1 = row.insertCell(1);
cell2 = row.insertCell(2);
cell3 = row.insertCell(3);
cell4 = row.insertCell(4);
cell5 = row.insertCell(5);
cell6 = row.insertCell(6);
cell7 = row.insertCell(7);
cell8 = row.insertCell(8);
cell.innerHTML = "<b>Title</b>";
cell1.innerHTML = "<b>Qualificatoin</b>";
cell2.innerHTML = "<b>Salary</b>";
cell3.innerHTML = "<b>Company Name</b>";
cell4.innerHTML = "<b>Tags</b>";
cell5.innerHTML = "<b>Location</b>";
cell6.innerHTML = "<b>Experience</b>";
cell7.innerHTML = "<b>About Company</b>";
cell8.innerHTML = "<b>Apply Here</b>";
var tableBody = document.createElement('TBODY');
table.appendChild(tableBody);
console.log("json length : "+obj.length);
for (var i=0; i<obj.length; i++){
/*var tr = document.createElement('TR');
tableBody.appendChild(tr);
var td = document.createElement('TD');
//td.width='75';
td.appendChild(document.createTextNode("Cell " + i + "," + j));
tr.appendChild(td);*/
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.firstName);
input.setAttribute("type", "hidden");
input.setAttribute("name", "jobID");
input.setAttribute("value", obj.firstName);
var rowCount = tableBody.getElementsByTagName("tr").length;
var row1 = tableBody.insertRow(rowCount);
row1.id="x";
var rowcell0=row1.insertCell(0);
var rowcell1=row1.insertCell(1);
var rowcell2=row1.insertCell(2);
var rowcell3=row1.insertCell(3);
var rowcell4=row1.insertCell(4);
rowcell4.id='doit';
var rowcell5=row1.insertCell(5);
var rowcell6=row1.insertCell(6);
var rowcell7=row1.insertCell(7);
var rowcell8=row1.insertCell(8);
var rowcell9=row1.insertCell(9);
rowcell0.innerHTML= obj[i].title;
rowcell1.innerHTML= obj[i].qualification;
rowcell2.innerHTML= obj[i].salary;
rowcell3.innerHTML=obj[i].companyName;
rowcell4.innerHTML=obj[i].tags;
rowcell5.innerHTML=obj[i].location;
rowcell6.innerHTML=obj[i].experience;
rowcell7.innerHTML=obj[i].aboutCompany;
//rowcell8.innerHTML=obj[i].firstName;
rowcell9.innerHTML=obj[i].jobID;
//document.getElementById("doit").appendChild(btn);
}myTableDiv.appendChild(table);
data_table=$('#dynamictable').DataTable({
"paging" : true,
"ordering" : true,
"info" : false,
"searching" : true,
"bDestroy" : true,
"columnDefs": [
{
"targets": [ 9 ],
"visible": false,
"searchable": false
},{
"targets": 8,
"data": null,
"defaultContent": "<button>Apply</button>"
}
]
});
$('#dynamictable tbody').on( 'click', 'button', function () {
var data = data_table.row( $(this).parents('tr') ).data();
var formData=data[9];
console.log("The data : "+data[9]);
$.ajax({
type : "GET",
url : "apply",
data:"jobID="+formData,
dataType : "text",
async: true,
success : function(response) {
console.log("The response : "+response);
}
});
});
};