获取所选下拉列表项的值 [英] Get value of selected drop down list item

查看:94
本文介绍了获取所选下拉列表项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Good Evening, i have these dynamically generated dropdown list in my 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>

并且我尝试了这些脚本来从这些下拉列表中获取选定的值:

and i tried these script to get the selected value from these dropdown :

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

这里的问题是,对于下拉菜单中选择的每个项目,它总是给出1,但是如果我将onChange更改为onchange="alert(this.value)",它将打印正确的值!怎么来的?以及如何获取在DropDown中选择的每个项目的实际索引

the problem here is that it always gives 1 for every item selected in the dropdown however i f i changed the onChange to be onchange="alert(this.value)" it prints the right values!! how that come? and how to get the actual index of every item selected in the DropDown

推荐答案

不完全清楚问题出在哪里,但这对我有用:

Not exactly sure what the problem is but this works for me:

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

演示: http://jsfiddle.net/louisbros/PS4zy/1/

这篇关于获取所选下拉列表项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆