0
我想從表格單元格中獲取Datalist的值。我有一個在表中的每個單元循環函數來獲取值:來自datalist內部表的訪問值
function getTableContents() {
//gets table
var oTable = document.getElementById('projectsTable');
var projectNames = [];
//gets rows of table
var rowLength = oTable.rows.length;
//loops through rows
for (i = 1; i < rowLength; i++){
//gets cells of current row
var oCells = oTable.rows.item(i).cells;
//gets amount of cells of current row
var cellLength = oCells.length;
//Push the html content of each cell into an array
projectNames.push(oCells.item(0).innerHTML);
}
//Loop through each stored cell and check whether that project exists in the database
for(i = 0; i < projectNames.length; i++){
checkUser(projectNames[i]);
}
}
是可以獲得單元格的值返回DataList控件的HTML,但不是實際值線。我可以用得到的值不同的方式:
document.getElementById("listOfProjects").value;
然而,這是不可取的,因爲使用相同的數據列表ID表可能可能包含多個數據列表場。我需要一些方法來獲取每個包含數據列表的單元格在單個單元格中的數據列表值。任何幫助,讚賞歡呼。
編輯:
在這裏我先定義數據列表:
$('.AddNew').click(function(){
"use strict";
$('#projectsTable tbody').append("<tr class='child'> \
<td><input type='text' id='listOfProjects' list='projectNames' placeholder='e.g. Project Name'> \
<datalist id='projectNames'> \
</datalist></td> \
<td> <input type='button' class='RemoveRow' value='Remove Item'> </td></tr>");
如果我更新內容的數據列表:
function getProjectNames(){
//Ensure function is only ran once. Otherwise it causes duplicates in the datalist
if(!projectNamesRetrieved){
var dataList = document.getElementById('projectNames');
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
yourval = JSON.parse(this.responseText);
document.getElementById("projectsresults").innerHTML = yourval;
for(var i = 0; i < yourval.length; i++) {
var carsOption = "<option value=\"" + yourval[i].Name + "\">" + yourval[i].Name + "</option>";
$('#projectNames').append(carsOption);
}
}
};
xmlhttp.open("GET","getProjectNames.php",true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(null);
projectNamesRetrieved = true;
}
}
感謝您的回覆,ive添加了與數據主義相關的其他2個函數,其中一個是我最初定義的數據列表,另一個是我填充選項的函數 – LazyAlpine