使用Javascript获取下拉列表中选定的值(显示null)
我需要使用Javascript访问从下拉列表中选择的值。但每次我得到“null”作为答案时,尽管选择了一个列表项 我的HTML页面:使用Javascript获取下拉列表中选定的值(显示null),javascript,html,select,drop-down-menu,getelementbyid,Javascript,Html,Select,Drop Down Menu,Getelementbyid,我需要使用Javascript访问从下拉列表中选择的值。但每次我得到“null”作为答案时,尽管选择了一个列表项 我的HTML页面: <select class="mySelect"> <option value="st1" selected="selected">Create new Stream</option> <option value="st1">Stream 1</option> <option
<select class="mySelect">
<option value="st1" selected="selected">Create new Stream</option>
<option value="st1">Stream 1</option>
<option value="st2">Stream 2</option>
<option value="st3">Stream 3</option>
<option value="st4">Stream 4</option>
</select>
<input type="button" value="show attributes" class="panel-button-attr" onclick="choice()">
在这里,我尝试使用第一个警报检查是否正确识别了任何选定的列表项。但是,在这一点上,它显示null,strUsr行根本不运行
我知道这实际上是一项微不足道的任务,但我发现很难找出这种不一致性 您必须指定select元素的
id
<select class="mySelect" id="mySelect">
您必须指定select元素的
id
<select class="mySelect" id="mySelect">
请更改HTML元素属性
您提到“mySelect”作为类,在JS中,您使用ID引用调用它。请更改您的HTML元素属性 您提到“mySelect”是类,在JS中,您使用ID引用调用它。按照代码进行操作: 您需要为下拉列表提供id,因为您试图通过id获取数据,所以
<select id="mySelect" class="mySelect">
<option value="st1" selected="selected">Create new Stream</option>
<option value="st1">Stream 1</option>
<option value="st2">Stream 2</option>
<option value="st3">Stream 3</option>
<option value="st4">Stream 4</option>
</select>
创建新流
流1
流2
流3
流4
我希望这能解决你的问题
谢谢…请按照以下代码操作:
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
});
您需要为下拉列表提供id,因为您试图通过id获取数据,所以
<select id="mySelect" class="mySelect">
<option value="st1" selected="selected">Create new Stream</option>
<option value="st1">Stream 1</option>
<option value="st2">Stream 2</option>
<option value="st3">Stream 3</option>
<option value="st4">Stream 4</option>
</select>
创建新流
流1
流2
流3
流4
我希望这能解决你的问题
谢谢…普朗克:
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
});
创建新流
流1
流2
流3
流4
函数选择(){
var choice=document.getElementById(“mySelect”);
警报(choice.value);//获取值
var strUser=choice.options[choice.selectedIndex].text;
警报(strUser.toString());
}
plunker:
创建新流
流1
流2
流3
流4
函数选择(){
var choice=document.getElementById(“mySelect”);
警报(choice.value);//获取值
var strUser=choice.options[choice.selectedIndex].text;
警报(strUser.toString());
}
您的html中没有id,因此我尝试按类名
function choice() {
var choice=document.getElementsByClassName("mySelect");
var strUser = choice[0].options[choice[0].selectedIndex].text;
alert(strUser.toString());
}
您的html中没有id,因此我尝试按类名
function choice() {
var choice=document.getElementsByClassName("mySelect");
var strUser = choice[0].options[choice[0].selectedIndex].text;
alert(strUser.toString());
}
如果getElementById返回null,则表示找不到该元素。您正试图访问id为“mySelect”的元素,但您在HTML中将“mySelect”作为类而不是id。@SpeedyInja否,简单的编码错误。另外,欢迎使用console.log over alert来享受更好的开发者体验。感谢所有的快速回复。糟糕的是,我已经为这个项目工作了好几个小时,甚至一些简单的事情似乎都被隐藏了起来。将其更改为ID后,它就可以工作了。如果getElementById返回null,则表示它没有找到元素。您正试图访问ID为“mySelect”的元素,但您在HTML中将“mySelect”作为类而不是ID指定。@SpeedyNinja否,简单的编码错误。另外,欢迎使用console.log over alert来享受更好的开发者体验。感谢所有的快速回复。糟糕的是,我已经为这个项目工作了好几个小时,甚至一些简单的事情似乎都被隐藏了起来。将其更改为ID,并且可以正常工作。请您详细说明您的答案,并添加有关您提供的解决方案的更多说明。请您详细说明您的答案,并添加有关您提供的解决方案的更多说明,好吗?