使用JSP我在单击添加按钮时添加包含select选项的单元格的表行。我需要从表格单元格中选择选定的值到Javascript。下面我添加了JSP和代码..
的Javascript
var maxID=0;
function getTemplateRow() {
var x = document.getElementById("templateRow").cloneNode(true);
x.id = "";
x.style.display = "";
x.cells[0].id = "staffno_add"+ ++maxID;
x.cells[1].id = "name_add"+ maxID;
x.cells[2].id = "desig_add"+ maxID;
x.cells[3].id = "curloc_add"+ maxID;
x.cells[1].style.visibility="hidden";
x.cells[2].style.visibility="hidden";
x.cells[3].setAttribute('onchange', 'getData(this)');
x.cells[2].setAttribute('onchange', 'getData1(this)');
return x;
}
function addRow(i) {
//var button=[];
var k=i.rowIndex;
var t = document.getElementById("theTable");
var rows = t.getElementsByTagName("tr");
var r = rows[parseInt(k)];
r.parentNode.insertBefore(getTemplateRow(), r);
}
var y1=null;
var y2=null;
function getData(dropdown) {
y1=dropdown.id;
y2=y1[10];
alert("desig_add"+y1[10]);
alert('curloc_add'+y2);
var get_id = document.getElementById('curloc_add'+y2).getElementsByClassName("getit")[0].innerText;//need my selected option value here...
alert(get_id);
document.getElementById("desig_add"+y2).style.visibility = "visible";
}
function getData1(dropdown1) {
y1=dropdown1.id;
y2=x1[9];
alert("name_add"+y1[9]);
document.getElementById("name_add"+y2).style.visibility = "visible";
}
JSP
<form name="myform">
<tr id="templateRow" style="display:none">
<td>
<input type="text" readonly>
</td>
<td>
<input type="text" readonly>
</td>
<td>
<select class="getit">
<option value="">None</option>
<%for(x1=0;x1<arrList.size();x1++){%>
<option value="<%=arrList.get(x1)%>"><%=arrList.get(x1)%></option>
<%}%>
</select>
</td>
<td>
<select >
<option value="">None</option>
<%for(x1=0;x1<lenList.size();x1++){%>
<option value="<%=lenList.get(x1)%>"><%=lenList.get(x1)%></option>
<%}%>
</select>
</td>
<td><input type="text" readonly></td>
<td><input type="text" class="datepicker" style="background-color:
#fffa90"/></td>
<td>
<input type="text" readonly>
</td>
<td>
<input type="text" class="datepicker" style="background-color: #fffa90"/>
</td>
<td><input type="text" class="datepicker" style="background-color:
#fffa90"/></td>
</tr>
<input type="hidden" id="total" name="total">
</form>
<tr onclick="addRow(this,);">
<td>
<input type = "button" id="insert" value="Add" >
</td>
</tr>`
我是JSP和Javascript的新手。我希望在脚本中选择选项值。