从更改时选择中获取选定的值/文本


95
<select onchange="test()" id="select_id">
    <option value="0">-Select-</option>
    <option value="1">Communication</option>
</select>

我需要在javascript中获取选定选项的值:有谁知道如何获取选定值或文本,请告诉如何为它编写函数。我已分配onchange()函数进行选择,因此之后该怎么办?



如果你只想要valueoption是被选中,然后... <select onchange="window.alert(this.value);">... optionš ... </select>...应该得到雅只是仅此而已。
S0AndS0

Answers:


114

为此使用JavaScript或jQuery。

使用JavaScript

<script>
function val() {
    d = document.getElementById("select_id").value;
    alert(d);
}
</script>

<select onchange="val()" id="select_id">

使用jQuery

$('#select_id').change(function(){
    alert($(this).val());
})

.value()在现代浏览器上有效,但在较旧的浏览器上无效。参见bytes.com/topic/javascript/answers/…–
Benissimo

2
@PlayHardGoPro这是选定的值。如果您想要文本(例如-Select-或Communication),则可以使用text:select.text或jQuery中select.text()
ricksmt

jQuery的末尾缺少分号... :)
lindhe

使用jQuery $('#select_id option:selected').text()。这将返回所选选项的文本,“选择”或“通讯”
Paulo Borralho Martins,2016年

1
香草javascript方法: document.getElementById("select_id").onchange = (evt) => { console.log(evt.srcElement.value); }
Spencer

44

如果您要对此进行搜索,并且不希望事件监听器成为属性,请使用:

document.getElementById('my-select').addEventListener('change', function() {
  console.log('You selected: ', this.value);
});
<select id="my-select">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>


30

function test(a) {
    var x = (a.value || a.options[a.selectedIndex].value);  //crossbrowser solution =)
    alert(x);
}
<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>


a.value为了什么?是否有任何实际支持的浏览器?我们不能只使用a.options[a.selectedIndex].value吗?
2012年

@Anonymous <以获取主题=)
El Dude

27

不需要onchange函数。您可以在一行中获取值:

document.getElementById("select_id").options[document.getElementById("select_id").selectedIndex].value;

或者,将其拆分以提高可读性:

var select_id = document.getElementById("select_id");

select_id.options[select_id.selectedIndex].value;

20

哇,答案中还没有真正可重用的解决方案。.我的意思是,一个标准事件处理程序应该只获取一个event参数,而根本不必使用id。

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中使用此处理程序:

<select onchange="handleSelectChange(event)">
    <option value="1">one</option>
    <option value="2">two</option>
</select>

jQuery的:

jQuery('#select_id').on('change',handleSelectChange);

或香草JS处理程序设置:

var selector = document.getElementById("select_id");
selector.onchange = handleSelectChange;
// or
selector.addEventListener('change', handleSelectChange);

并且不必为select您拥有的每个元素都重写它。

示例片段:

function handleSelectChange(event) {

    var selectElement = event.target;
    var value = selectElement.value;
    alert(value);
}
<select onchange="handleSelectChange(event)">
    <option value="1">one</option>
    <option value="2">two</option>
</select>


2
很好的解决方案,应该接受。使用纯事件元素而不引用DOM文档是最灵活的方法,它可以在许多环境中工作,例如React,Vue或纯HTML表单。
VanDavv

8
let dropdown = document.querySelector('select');
if (dropdown) dropdown.addEventListener('change', function(event) {
    console.log(event.target.value);
});

如果使用箭头功能,则此方法很有用。
弗朗西

任何箭头函数都可以替换为function(){},这是同一回事
Russell Strauss

不一样 this箭头函数从其词法环境中获取其上下文,但普通函数具有其自身的上下文。检查一下
Franchy

6

使用

document.getElementById("select_id").selectedIndex

或获得价值:

document.getElementById("select_id").value

这种获取价值的方法在旧的浏览器中不起作用,请改用Danny的解决方案
wlf

5
<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>

在警报中,您将看到所选索引的INT值,将所选内容视为数组,您将获得该值


5

HTML:

<select onchange="cityChanged(this.value)">
      <option value="CHICAGO">Chicago</option>
      <option value="NEWYORK">New York</option>
</select>

JS:

function cityChanged(city) {
    alert(city);
}

4

我想知道每个人都发表了有关的选择valuetext选择的信息<option>,但没有人建议label

所以我也建议label所有浏览器都支持

获得value(与其他人建议的相同)

function test(a) {
var x = a.options[a.selectedIndex].value;
alert(x);
}

获得option text(即通信或-Select-)

function test(a) {
var x = a.options[a.selectedIndex].text;
alert(x);
}

(新建议)

function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}

的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>

注意:在上面的HTML中,option值为2,label将返回newText而不是Communication

注意:无法在Firefox中设置label属性(仅返回)。


3

这是一个古老的问题,但是我不确定为什么人们不建议使用事件对象来检索信息,而不是再次搜索DOM。

只需在函数onChange中遍历事件对象,请参见下面的示例

function test() { console.log(event.srcElement.value); }

http://jsfiddle.net/Corsico/3yvh9wc6/5/

如果这不是7年前的默认行为,可能对今天查找的人有用


2

function test(){
  var sel1 = document.getElementById("select_id");
  var strUser1 = sel1.options[sel1.selectedIndex].value;
  console.log(strUser1);
  alert(strUser1);
  // Inorder to get the Test as value i.e "Communication"
  var sel2 = document.getElementById("select_id");
  var strUser2 = sel2.options[sel2.selectedIndex].text;
  console.log(strUser2);
  alert(strUser2);
}
<select onchange="test()" id="select_id">
  <option value="0">-Select-</option>
  <option value="1">Communication</option>
</select>


0

function test(){
  var sel1 = document.getElementById("select_id");
  var strUser1 = sel1.options[sel1.selectedIndex].value;
  console.log(strUser1);
  alert(strUser1);
  // Inorder to get the Test as value i.e "Communication"
  var sel2 = document.getElementById("select_id");
  var strUser2 = sel2.options[sel2.selectedIndex].text;
  console.log(strUser2);
  alert(strUser2);
}
<select onchange="test()" id="select_id">
  <option value="0">-Select-</option>
  <option value="1">Communication</option>
</select>

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

-1

我尝试用自己的样本进行解释,但希望对您有所帮助。您不需要onchange =“ test()”。请运行代码段以获取实时结果。

document.getElementById("cars").addEventListener("change", displayCar);

function displayCar() {
  var selected_value = document.getElementById("cars").value;
  alert(selected_value);
}
<select id="cars">
  <option value="bmw">BMW</option>
  <option value="mercedes">Mercedes</option>
  <option value="volkswagen">Volkswagen</option>
  <option value="audi">Audi</option>
</select>


进行更改比单击事件要好得多,因为选择一个选项并没有什么危险。
莱利·卡尼

@RileyCarney好吧,它取决于,而不是根据js部分上的函数名称更改来更改ui部分上的重构代码,它更加优雅,而且顺便说一句,我没有看到任何click事件。

您对其进行了编辑,以删除onclick事件haha。如此阴暗,在您单击之前,如果您单击汽车通知,则会进行通知。edited 7 hours ago至少您修复了无法正常运行的代码。
莱利·卡尼
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.