从 Select on change 获取选定的值/文本

IT技术 javascript
2021-01-30 08:00:40
<select onchange="test()" id="select_id">
    <option value="0">-Select-</option>
    <option value="1">Communication</option>
</select>

我需要在 javascript 中获取所选选项的值:有谁知道如何获取所选值或文本,请告诉如何为其编写函数。我已经分配了 onchange() 函数来选择,那之后我该怎么办?

6个回答

为此,请使用 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());
})
香草javascript方法: document.getElementById("select_id").onchange = (evt) => { console.log(evt.srcElement.value); }
2021-03-14 08:00:40
@PlayHardGoPro 那是选定的值。如果您想要文本(例如 -Select- 或 Communication),您可以使用 text:select.text或在 jQuery 中select.text()
2021-04-02 08:00:40
.value() 适用于现代浏览器,但不适用于真正的旧浏览器。请参阅bytes.com/topic/javascript/answers/...
2021-04-07 08:00:40
使用 jQuery $('#select_id option:selected').text()。这将返回所选选项的文本,选择通信
2021-04-11 08:00:40

如果你在谷歌上搜索这个,并且不希望事件侦听器成为一个属性,请使用:

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>

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吗?
2021-03-15 08:00:40
@Anonymous< 获取 subj =)
2021-04-11 08:00:40

哇,答案中还没有真正可重用的解决方案......我的意思是,一个标准的事件处理程序应该只得到一个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>

很好的解决方案,应该被接受。使用纯事件元素,不引用 DOM 文档是最灵活的方式,它适用于许多环境,如 React、Vue 或纯 HTML 表单。
2021-03-23 08:00:40
最好的解决方案!谢谢 !
2021-04-07 08:00:40

不需要 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;