从更改时选择中获取选定的值/文本 [英] Get selected value/text from Select on change

查看:103
本文介绍了从更改时选择中获取选定的值/文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I need to get the value of the selected option in javascript: does anyone know how to get the selected value or text, please tell how to write a function for it. I have assigned onchange() function to select so what do i do after that?

推荐答案

使用JavaScript或jQuery。

Use either JavaScript or jQuery for this.

使用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());
})

这篇关于从更改时选择中获取选定的值/文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆