jQuery 如何在jQuery中获取选择框选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12489527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:44:47  来源:igfitidea点击:

How to get select box option value in jQuery

jqueryhtml-select

提问by jayu patel

How to get the value of option select box in jQuery if I have the code like this,

如果我有这样的代码,如何在 jQuery 中获取选项选择框的值,

<select id='media' name='media'>
    <option value='1'>media1</option>
    <option value='2'>media2</option>
    <option value='3'>media3</option>
</select>

When I code for on change event,

当我为 on change 事件编码时,

$(document).ready(function()
{
    $("#media").change(function()
    {
        var id=$(this).val();
        var dataString = 'id='+ id;
        alert(id); return false;
    });
});

It gives me media1,media2,media3instead of 1, 2, 3

它给了我media1, media2,media3而不是1, 2, 3

How to get the value 1, 2, 3?

如何获得值 1、2、3?

回答by SureshKumaran

$("#id option:selected").val();

Here idis the id of a select box. I'm sure it will work 100%.

id是选择框的 id。我相信它会 100% 工作。

If you want selected text use this one:

如果你想选择文本使用这个:

$("#id option:selected").text();

回答by Rahul

$('#media').change(function(){
alert($(this).val());
});

this should work the way you want it should.

这应该按照您希望的方式工作。

回答by Dhrumil

try this,

尝试这个,

jQuery(document).ready(function() {

    jQuery('#media').change(function(e) {

        var id = $(this,':selected').val();
        alert(id);

    });

});

回答by Samuel Santos

I did so,

我这样做了,

<select id='coprede' name='cop_rede'>
<option value='1'>Cop Rio de Janeiro</option>
    <option value='2'>Cop Nordeste</option>
    <option value='3'>Cop Vitoria</option>
    <option value='4'>Cluster Norte</option>
    <option value='5'>Cluster Centro Oeste</option>
</select>

within the script:

在脚本中:

$(function(){

$('#coprede').change(function(){
    alert($('#coprede :selected').val());
    });
});

回答by Anant Dabhi

make sure you use latest jquery because its working fine

确保您使用最新的 jquery,因为它工作正常

http://jsfiddle.net/Hayhv/

http://jsfiddle.net/Hayhv/

check it give value of option

检查它给出选项的值

回答by Pragnesh Chauhan

use this code:

使用此代码:

$('#media').change(function(){
    alert($('#media :selected').val());// or $('#media').val();        
});?

回答by Bharat Chodvadiya

Use class instead of id like this..

像这样使用 class 而不是 id ..

<select class='media' name='media'>
    <option value='1'>media1</option>
    <option value='2'>media2</option>
    <option value='3'>media3</option>
</select>

$(document).ready(function(){
    $(".media").live("change",function(){
        var id = $(this).val();
        var dataString = 'id='+ id;
        alert(id); return false;
    }); 
}); 

回答by Hkachhia

I have tested your code and its works ok for me make sure about id #media does repeat from other control. Because its config and get value from last find html control

我已经测试了你的代码,它对我来说工作正常,确保 id #media 确实从其他控件中重复。因为它的 config 和 get value from last find html control