var as = document.form1.ddlViewBy.value;
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
var value = document.getElementById("ddlViewBy").value;
var res = document.querySelector('p');
var select = document.querySelector('select');
res.textContent = "Selected Value: "+select.value;
select.addEventListener("change", function(e){
res.textContent = "Selected Value: "+select.value;
});
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Change value of the select to show the value.
<p>
</p>
The following script is getting the value of the selected option and putting it in text box 1
<script>
function run() {
document.getElementById("srt").value = document.getElementById("Ultra").value;
}
</script>
The following script is getting a value from a text box 2 and alerting with its value
<script>
function up() {
//if (document.getElementById("srt").value != "") {
var dop = document.getElementById("srt").value;
//}
alert(dop);
}
</script>
The following script is calling a function from a function
<script>
function up() {
var dop = document.getElementById("srt").value;
pop(dop); // Calling function pop
}
function pop(val) {
alert(val);
}?
</script>
var getValue = document.getElementById('ddlViewBy').selectedOptions[0].value;
alert (getValue); // This will output the value selected.
OR
var ddlViewBy = document.getElementById('ddlViewBy');
var value = ddlViewBy.options[ddlViewBy.selectedIndex].value;
var text = ddlViewBy.options[ddlViewBy.selectedIndex].text;
alert (value); // this will output the value selected
alert (text); // this will output the text of the value selected
JQuery:
$("#ddlViewBy:selected").text(); // Text of the selected value
$("#ddlViewBy").val(); // Outputs the value of the ID in 'ddlViewBy'
11
如果您遇到纯粹为IE编写的代码,您可能会看到:
var e = document.getElementById("ddlViewBy");
var strUser = e.options(e.selectedIndex).value;
22 回答
这是在onchange函数中执行此操作的简单方法:
event.target.options[event.target.selectedIndex].dataset.name
初学者可能希望使用NAME属性而不是ID属性来访问select中的值 . 我们知道所有表单元素都需要名称,甚至在它们获得ID之前 .
所以,我正在为新开发人员添加
getElementByName()
解决方案 .NB . 表单元素的名称必须是唯一的,表单一旦发布就可以使用,但DOM可以允许名称由多个元素共享 . 因此,如果可以,请考虑向表单添加ID,或者明确表单元素名称
my_nth_select_named_x
和my_nth_text_input_named_y
.使用
getElementByName
的示例:由于可能性,代码的直观性以及
id
与name
的使用,之前的答案仍有待改进的空间 . 可以读出所选选项的三个数据 - 索引号,值和文本 . 这个简单的跨浏览器代码可以完成所有三个:现场演示:http://jsbin.com/jiwena/1/edit?html,output .
id
应该用于化妆目的 . 出于功能形式的目的,name
仍然有效,同样在HTML5中,仍然应该使用 . 最后,请注意在某些地方使用正方形与圆形括号 . 如前所述,只有(旧版本)IE将在所有地方接受圆形 .为了得到上述答案,这就是我作为一个单行的方式 . 这是为了获取所选选项的实际文本 . 已经有很好的例子来获取索引号 . (对于文本,我只是想以这种方式显示)
在极少数情况下,您可能需要使用括号,但这种情况非常罕见 .
我怀疑这个过程比两行版本更快 . 我只想尽可能地整合我的代码 .
如果有人知道如何在一行中完成这一操作而不必两次获得该元素,我很乐意为您发表评论并告诉我如何 . 我还没弄清楚......
只是用
$('#SelectBoxId option:selected').text();
用于获取列出的文本$('#SelectBoxId').val();
用于获取所选索引值运行如何工作的示例:
注意:值不会随着下拉列表的更改而更改,如果您需要该功能,则需要实现onClick更改 .
这是一个JavaScript代码行:
假设名为list
name="list"
的下拉菜单包含在名称属性为name="form1"
的表单中 .我有点不同的看法,如何实现这一目标 . 我通常使用以下方法执行此操作:(就我所知,它更简单,适用于所有浏览器 . )
您可以使用
selectElement.value
获取所选选项的值 .请参阅文章Selecting dropdown element using JavaScript or jQuery . 他们使用JavaScript和jQuery以多种方式解释了它 .
使用jQuery:
你可以使用
querySelector
.E.g.
普通的JavaScript:
jQuery的:
AngularJS:(104473):
以下代码展示了与使用JavaScript从输入/选择字段获取/放置值相关的各种示例 .
Sorurce Link
工作Demo
The following script is getting the value of the selected option and putting it in text box 1
The following script is getting a value from a text box 2 and alerting with its value
The following script is calling a function from a function
这是正确的,应该给你 Value . 这是你要追求的文字吗?
所以你对术语很清楚:
此选项包括:
指数= 0
Value =你好
Text = Hello World
我能看到这个代码不起作用的唯一原因是你使用的是IE7-,而忘了为你的
<option>
-tags指定value属性...其他每个浏览器都应该将open-close标签中的内容转换为选项值 .2015年,在Firefox,以下也有效 .
有两种方法可以使用
JavaScript
或JQuery
完成此操作 .JavaScript:
OR
JQuery:
如果您遇到纯粹为IE编写的代码,您可能会看到:
在Firefox等运行上面的内容会给你一个'不是函数'的错误,因为IE允许你使用()而不是[]来逃避:
正确的方法是使用方括号 .
如果你有一个如下所示的select元素:
运行此代码:
会使
strUser
成为2
. 如果你真正想要的是test2
,那么这样做:哪个会让
strUser
成为test2
另一个解决方案是