jQuery获取select onChange的值 [英] jQuery get value of select onChange

查看:83
本文介绍了jQuery获取select onChange的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是,可以通过执行$(this).val();并将onchange参数应用于选择字段来获取选择输入的值.

I was under the impression that I could get the value of a select input by doing this $(this).val(); and applying the onchange parameter to the select field.

似乎只有在我引用ID的情况下,它才有效.

It would appear it only works if I reference the ID.

如何使用此功能.

推荐答案

尝试一下-

$('select').on('change', function() {
  alert( this.value );
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select>
    <option value="1">One</option>
    <option value="2">Two</option>
</select>

您还可以引用onchange事件-

You can also reference with onchange event-

function getval(sel)
{
    alert(sel.value);
}

<select onchange="getval(this);">
    <option value="1">One</option>
    <option value="2">Two</option>
</select>

这篇关于jQuery获取select onChange的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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