Javascript 从更改时选择获取选定值/文本 -挑选- 沟通
我需要获取javascript中所选选项的值:是否有人知道如何获取所选值或文本,请告诉我如何为其编写函数。我已经分配了onchange()函数来选择,那么接下来我该怎么做呢?为此使用JavaScript或jQuery 使用JavaScriptJavascript 从更改时选择获取选定值/文本 -挑选- 沟通,javascript,Javascript,我需要获取javascript中所选选项的值:是否有人知道如何获取所选值或文本,请告诉我如何为其编写函数。我已经分配了onchange()函数来选择,那么接下来我该怎么做呢?为此使用JavaScript或jQuery 使用JavaScript <select onchange="test()" id="select_id"> <option value="0">-Select-</option> <option value="1">
<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
使用
或获取值:
document.getElementById("select_id").selectedIndex
功能测试(a){
var x=a.选择的指数;
警报(x);
}
-挑选-
沟通
沟通
沟通
在警报中,您将看到所选索引的INT值,将所选内容视为一个数组,您将得到该值
功能测试(a){
var x=(a.value | | a.options[a.selectedIndex].value);//交叉浏览器解决方案=)
警报(x);
}
-挑选-
沟通
沟通
沟通
不需要onchange函数。您可以在一行中获取值:
<script>
function test(a) {
var x = a.selectedIndex;
alert(x);
}
</script>
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2">Communication</option>
<option value="3">Communication</option>
</select>
或者,将其拆分以提高可读性:
document.getElementById("select_id").options[document.getElementById("select_id").selectedIndex].value;
如果您在谷歌上搜索此项,并且不希望事件侦听器成为属性,请使用:
document.getElementById('my-select').addEventListener('change',function(){
console.log('您选择:',此.value);
});代码>
一个
两个
三
功能测试(){
var sel1=document.getElementById(“选择id”);
var struster1=sel1.options[sel1.selectedIndex].value;
控制台日志(struster1);
警报(struster1);
//为了获得测试值,即“通信”
var sel2=document.getElementById(“选择id”);
var strUser2=sel2.options[sel2.selectedIndex].text;
控制台日志(struster2);
警报(Struster2);
}
-挑选-
沟通
哇,答案中还没有真正可重用的解决方案。。我的意思是,一个标准的事件处理程序应该只获得一个事件参数,而不必使用id。。我会使用:
var select_id = document.getElementById("select_id");
select_id.options[select_id.selectedIndex].value;
您可以将此处理程序用于每个–inline js:
function handleSelectChange(event) {
// if you want to support some really old IEs, add
// event = event || window.event;
var selectElement = event.target;
var value = selectElement.value;
// to support really old browsers, you may use
// selectElement.value || selectElement.options[selectElement.selectedIndex].value;
// like el Dude has suggested
// do whatever you want with value
}
或JS处理程序设置:
jQuery('#select_id').on('change',handleSelectChange);
并且不必为每个选择元素重写此代码
示例代码段:
函数handleSelectChange(事件){
var selectElement=event.target;
var value=selectElement.value;
警报(值);
}
一
二
我想知道每个人都发布了关于值和文本的选项,从获取信息,但没有人建议使用标签
所以我也建议使用标签
,因为所有浏览器都支持它
获取值
(与其他建议相同)
获取选项
文本
(即通信或-选择-)
或(新建议)
HTML
function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}
-挑选-
沟通
沟通
注意:在上面的选项
值2的HTML中,标签
将返回新文本,而不是通信
也
注意:无法在Firefox中设置label属性(仅返回)
功能测试(){
var sel1=document.getElementById(“选择id”);
var struster1=sel1.options[sel1.selectedIndex].value;
控制台日志(struster1);
警报(struster1);
//为了获得测试值,即“通信”
var sel2=document.getElementById(“选择id”);
var strUser2=sel2.options[sel2.selectedIndex].text;
控制台日志(struster2);
警报(Struster2);
}
-挑选-
沟通
我试着用我自己的示例来解释,但我希望它能帮助您。您不需要onchange=“test()”请运行代码片段以获得实时结果
document.getElementById(“cars”).addEventListener(“更改”,显示CAR);
函数displayCar(){
var selected_value=document.getElementById(“cars”).value;
警报(所选的_值);
}
宝马
梅赛德斯
大众
奥迪
这是一个老问题,但我不确定为什么人们不建议使用事件对象来检索信息,而不是再次搜索DOM
只需在函数onChange中遍历事件对象,参见下面的示例
功能测试(){
log(event.srcielement.value);
}
如果这不是7年前的默认行为,可能对今天查找此内容的人很有用HTML:
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2" label=‘newText’>Communication</option>
</select>
$('select#id')。更改(函数(){
//选定值
警报($(this.val());
//选定文本
警报($(this).find(“选项:选定”).text();
})
-挑选-
沟通
Here:和Here:如果您只需要选择的选项
的值
,那么<代码>
<代码>选项
s..
。。。你应该只得到这个,什么都没有。什么是a.value
?有没有真正支持这种功能的浏览器?我们不能只使用a.options[a.selectedIndex].value
?@Anonymous<来获取subm=).value()在现代浏览器上有效,但在真正的旧浏览器上不起作用。请参见@PlayHardGoPro,这是选定的值。如果需要文本(例如-Select-或Communication),可以使用文本:Select.text
或在jQuerySelect.text()
。jQuery结尾缺少分号…:)使用jQuery$('#select_id option:selected').text()
。这将返回所选选项、select或communication的文本。javascript方法:document.getElementById(“select_id”).onchange=(evt)=>{console.log(evt.srcement.value);}
这种获取值的方法在旧浏览器中不起作用,用丹尼的解决方案代替伟大的解决方案,应该被接受。使用纯事件元素,不引用DOM文档是最灵活的方法,它可以在许多环境中工作,如React、Vue或纯HTML表单
<select onchange="handleSelectChange(event)">
<option value="1">one</option>
<option value="2">two</option>
</select>
jQuery('#select_id').on('change',handleSelectChange);
var selector = document.getElementById("select_id");
selector.onchange = handleSelectChange;
// or
selector.addEventListener('change', handleSelectChange);
function test(a) {
var x = a.options[a.selectedIndex].value;
alert(x);
}
function test(a) {
var x = a.options[a.selectedIndex].text;
alert(x);
}
function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2" label=‘newText’>Communication</option>
</select>
<select onchange="cityChanged(this.value)">
<option value="CHICAGO">Chicago</option>
<option value="NEWYORK">New York</option>
</select>
function cityChanged(city) {
alert(city);
}
let dropdown = document.querySelector('select');
if (dropdown) dropdown.addEventListener('change', function(event) {
console.log(event.target.value);
});