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

Siv*_*va 63 javascript

<select onchange="test()" id="select_id">
    <option value="0">-Select-</option>
    <option value="1">Communication</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我需要在javascript中获取所选选项的值:有没有人知道如何获取所选值或文本,请告诉我们如何为它编写函数.我已经分配了onchange()函数来选择那么我之后做了什么?

Hus*_*ein 92

为此使用JavaScript或jQuery.

使用JavaScript

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

<select onchange="val()" id="select_id">
Run Code Online (Sandbox Code Playgroud)

使用jQuery

$('#select_id').change(function(){
    alert($(this).val());
})
Run Code Online (Sandbox Code Playgroud)

  • vanilla javascript 方法:```document.getElementById("select_id").onchange = (evt) =&gt; { console.log(evt.srcElement.value); }``` (6认同)
  • @PlayHardGoPro这是选定的值.如果你想要文本(例如-Select-或Communication),你可以使用text:`select.text`或jQuery`select.text()`. (2认同)

小智 24

不需要onchange功能.您可以在一行中获取值:

document.getElementById("select_id").options[document.getElementById("select_id").selectedIndex].value;
Run Code Online (Sandbox Code Playgroud)

或者,将其拆分以获得更好的可读性:

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

select_id.options[select_id.selectedIndex].value;
Run Code Online (Sandbox Code Playgroud)


el *_*ude 21

function test(a) {
    var x = (a.value || a.options[a.selectedIndex].value);  //crossbrowser solution =)
    alert(x);
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*n G 18

如果您正在使用Google搜索,并且不希望事件侦听器成为属性,请使用:

document.getElementById('select_id').addEventListener('change', function(){
    this.value;
});
Run Code Online (Sandbox Code Playgroud)

  • 在某些情况下,函数作用域发生变化时,“this”不起作用。最好做 `const mySelect = document.getElementById('my-select'); mySelect.addEventListener('change', function() { console.log('您选择了:', mySelect.value);});` (4认同)

Rus*_*uss 14

let dropdown = document.querySelector('select');
if (dropdown) dropdown.addEventListener('change', function(event) {
    console.log(event.target.value);
});
Run Code Online (Sandbox Code Playgroud)


Yak*_*ovL 13

哇,答案中还没有真正可重用的解决方案。.我的意思是,一个标准的事件处理程序应该只获取一个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
}
Run Code Online (Sandbox Code Playgroud)

您可以在每个内联js中使用此处理程序:

<select onchange="handleSelectChange(event)">
    <option value="1">one</option>
    <option value="2">two</option>
</select>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

jQuery('#select_id').on('change',handleSelectChange);
Run Code Online (Sandbox Code Playgroud)

或香草JS处理程序设置:

var selector = document.getElementById("select_id");
selector.onchange = handleSelectChange;
// or
selector.addEventListener('change', handleSelectChange);
Run Code Online (Sandbox Code Playgroud)

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

示例片段:

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
}
Run Code Online (Sandbox Code Playgroud)
<select onchange="handleSelectChange(event)">
    <option value="1">one</option>
    <option value="2">two</option>
</select>
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案,应该被接受。使用纯事件元素,不引用 DOM 文档是最灵活的方式,它适用于许多环境,如 React、Vue 或纯 HTML 表单。 (2认同)

OZZ*_*ZIE 8

为什么让它过于复杂:

var select = document.querySelector('select#id.orClass');
select.addEventListener('change', function(e) {
  console.log(select.value);

  // or if it changes dynamically
  console.log(e.target.value);      
});
Run Code Online (Sandbox Code Playgroud)

var select = document.querySelector('select#id.orClass');
select.addEventListener('change', function(e) {
  console.log(select.value);

  // or if it changes dynamically
  console.log(e.target.value);      
});
Run Code Online (Sandbox Code Playgroud)
 let select = document.getElementById('select_id');
  select.addEventListener('change', function() {
    console.log(select.value);
    // just for test
    alert(select.value);
  });
Run Code Online (Sandbox Code Playgroud)


Ale*_*der 7

HTML:

<select onchange="cityChanged(this.value)">
      <option value="CHICAGO">Chicago</option>
      <option value="NEWYORK">New York</option>
</select>
Run Code Online (Sandbox Code Playgroud)

JS:

function cityChanged(city) {
    alert(city);
}
Run Code Online (Sandbox Code Playgroud)


MiP*_*mic 6

<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>
Run Code Online (Sandbox Code Playgroud)

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


Moh*_*lah 6

        $('#select_id').change(function(){
        // selected value 
        alert($(this).val());
        // selected text 
        alert($(this).find("option:selected").text());
    })
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<select onchange="test()" id="select_id">
        <option value="0">-Select-</option>
        <option value="1">Communication</option>
    </select>
Run Code Online (Sandbox Code Playgroud)


Clo*_*ble 5

使用

document.getElementById("select_id").selectedIndex
Run Code Online (Sandbox Code Playgroud)

或者获得价值:

document.getElementById("select_id").value
Run Code Online (Sandbox Code Playgroud)


Sur*_* SD 5

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

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

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

function test(a) {
var x = a.options[a.selectedIndex].value;
alert(x);
}
Run Code Online (Sandbox Code Playgroud)

获取option text(即通信或-选择-)

function test(a) {
var x = a.options[a.selectedIndex].text;
alert(x);
}
Run Code Online (Sandbox Code Playgroud)

(新建议)

function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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