jQuery获取select onChange的值

本文翻译自:jQuery get value of select onChange

I was under the impression that I could get the value of a select input by doing this $(this).val(); 我的印象是我可以通过执行$(this).val();来获取选择输入的值$(this).val(); and applying the onchange parameter to the select field. 并将onchange参数应用于选择字段。

It would appear it only works if I reference the ID. 它似乎只有在我引用ID时才有效。

How do I do it using this. 我该怎么做呢。


#1楼

参考:https://stackoom.com/question/kuH0/jQuery获取select-onChange的值


#2楼

Try this- 试试这个-

 $('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> 

You can also reference with onchange event- 你也可以参考onchange事件 -

 function getval(sel) { alert(sel.value); } 
 <select onchange="getval(this);"> <option value="1">One</option> <option value="2">Two</option> </select> 


#3楼

I was under the impression that I could get the value of a select input by doing this $(this).val(); 我的印象是我可以通过执行$(this).val();来获取选择输入的值。

This works if you subscribe unobtrusively (which is the recommended approach): 如果您不引人注意地订阅(这是推荐的方法),这是有效的:

$('#id_of_field').change(function() {
    // $(this).val() will work here
});

if you use onselect and mix markup with script you need to pass a reference to the current element: 如果使用onselect并将标记与脚本混合,则需要传递对当前元素的引用:

onselect="foo(this);"

and then: 然后:

function foo(element) {
    // $(element).val() will give you what you are looking for
}

#4楼

Look for jQuery site 寻找jQuery 网站

HTML: HTML:

<form>
  <input class="target" type="text" value="Field 1">
  <select class="target">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
  </select>
</form>
<div id="other">
  Trigger the handler
</div>

JAVASCRIPT: JAVASCRIPT:

$( ".target" ).change(function() {
  alert( "Handler for .change() called." );
});

jQuery's example: jQuery的例子:

To add a validity test to all text input elements: 要为所有文本输入元素添加有效性测试:

$( "input[type='text']" ).change(function() {
  // Check input( $( this ).val() ) for validity here
});

#5楼

Try the event delegation method, this works in almost all cases. 尝试使用事件委派方法,几乎​​适用于所有情况。

$(document.body).on('change',"#selectID",function (e) {
   //doStuff
   var optVal= $("#selectID option:selected").val();
});

#6楼

This is helped for me. 这对我有帮助。

For select: 对于选择:

$('select_tags').on('change', function() {
    alert( $(this).find(":selected").val() );
});

For radio/checkbox: 对于收音机/复选框:

$('radio_tags').on('change', function() {
    alert( $(this).find(":checked").val() );
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值