获取选择下拉列表项的值

问题描述 投票:2回答:2

晚上好,我在我的JSP这些动态生成的下拉列表:`

    <td><select name="model" id="model"  onchange="convertDropDownToTextBox()">

                            <c:forEach items="${model_list}" var="item">

                                <option value="${item.modelId}">${item.modelName}</option>
                            </c:forEach>
                                                            <option value="100">Add New Model</option>

            </select></td>

我尝试了这些脚本来得到这些下拉列表中选择的值:

function convertDropDownToTextBox(){
    var select = document.getElementById("model");
    var selectedString = select.options[select.selectedIndex].value;
    alert(selectedString);
}

这里的问题是,它总是给1在下拉列表中选择每个项目然而I F我改变了的onChange是onchange="alert(this.value)"它打印正确的价值观!怎么说来的?以及如何获得每个项目的下拉选择的实际指数

javascript onchange html-select
2个回答
4
投票

不完全知道什么问题,但是这对我的作品:

var select = document.getElementById("model");
select.onchange = function(){
    var selectedString = select.options[select.selectedIndex].value;
    alert(selectedString);
}

但是:四联zxsw POI


0
投票

只需通过http://jsfiddle.net/louisbros/PS4zy/1/对象到端点的功能:

event

然后你可以从函数中访问值<select onchange="convertDropDownToTextBox(event)"> <option value="1">Option 1</option> </select>

event.target.value
© www.soinside.com 2019 - 2024. All rights reserved.