Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

jquery - 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.

(我该怎么做呢。)

  ask by Robin Knight translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...