简体   繁体   中英

change selected value of dynamically generated dropdown list

I have a dynamic drop down list generated in a JSP page as follow:

<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.

I don't see anything in your HTML with an id of model . Did you mean for the input with a name of model to also have that as an id?

I also notice there is no ending </tr> in your second table.

Don't use var rowData = new Array(); use literal notation: var rowData = []; .

After adding on the id, adding a couple <tr> s, it worked for me fine:

http://jsfiddle.net/FJfZK/

Note, that your rendered HTML output is much more helpful when debugging a JavaScript issue. We generally don't care what your server is doing, when it is a DOM manipulation issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM