如何在jQuery中获取选择框选项值 [英] How to get select box option value in jQuery
本文介绍了如何在jQuery中获取选择框选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的代码,如何在jQuery中获取选项选择框的值,
How to get the value of option select box in jQuery if I have the code like this,
<select id='media' name='media'>
<option value='1'>media1</option>
<option value='2'>media2</option>
<option value='3'>media3</option>
</select>
当我为on change事件编写代码时,
When I code for on change event,
$(document).ready(function()
{
$("#media").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
alert(id); return false;
});
});
它给了我media1
,media2
,media3
而不是1, 2, 3
如何获取值1、2、3?
How to get the value 1, 2, 3?
推荐答案
$("#id option:selected").val();
此处id
是选择框的ID.我敢肯定它会100%工作.
Here id
is the id of a select box. I'm sure it will work 100%.
如果要选择文本,请使用以下文本:
If you want selected text use this one:
$("#id option:selected").text();
这篇关于如何在jQuery中获取选择框选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文