[英]change selected value of dynamically generated dropdown list
I have a dynamic drop down list generated in a JSP page as follow: 我在JSP页面中生成了一个动态下拉列表,如下所示:
<table BORDER=4 BORDERCOLOR=ORANGE width="300px">
<tr>
<td>Model:</td>
<td><select name="model" id="model">
<c:forEach items="${model_list}" var="item">
<option value="${item.modelId}">${item.modelName}</option>
</c:forEach>
</select></td>
</tr>
</table>
I just want to change the selected value of these drop down based on another value that I get from here: 我只想根据我从这里获得的另一个值来更改这些下拉列表的选定值:
<table BORDER=4 BORDERCOLOR=ORANGE width="120px" id="product_table">
<c:forEach items="${product_list}" var="car">
<tr>
<td><INPUT type="checkbox" name="chk_group" value="${car.carId}" /></td>
<td><c:out value="${car.carId}" /></td>
<td><c:out value="${car.model.modelName}" /></td>
<td><c:out value="${car.model.modelId}" /></td>
</table>
And here's the script: 这是脚本:
function findRowNumber() {
var rowIdx;
var rowData = new Array();
var table = document.getElementById('product_table');
var rows = table.getElementsByTagName('tr');
var selectedRow;
for ( var i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
rowIdx = this.rowIndex;
selectedRow = rows[rowIdx];
document.getElementById('model').value = selectedRow.cells[3].innerHTML;
}
}
}
Note: selectedRow.cells[3].innerHTML
returns 3 but the value doesn't change. 注意:
selectedRow.cells[3].innerHTML
返回3但值不会改变。
I don't see anything in your HTML with an id of model
. 我在HTML中看不到任何带有
model
ID的内容。 Did you mean for the input with a name of model
to also have that as an id? 您是否将具有
model
名称的输入也称为id?
I also notice there is no ending </tr>
in your second table. 我还注意到你的第二张表中没有结尾
</tr>
。
Don't use var rowData = new Array();
不要使用
var rowData = new Array();
use literal notation: var rowData = [];
使用文字表示法:
var rowData = [];
. 。
After adding on the id, adding a couple <tr>
s, it worked for me fine: 在添加了id之后,添加了几个
<tr>
,它对我很有用:
http://jsfiddle.net/FJfZK/ http://jsfiddle.net/FJfZK/
Note, that your rendered HTML output is much more helpful when debugging a JavaScript issue. 请注意,调试JavaScript的一个问题,当你渲染HTML输出更加有用。 We generally don't care what your server is doing, when it is a DOM manipulation issue.
当它是DOM操作问题时,我们通常不关心您的服务器正在做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.