jquery 选择选定的值未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19376899/
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 23:49:42  来源:igfitidea点击:

jquery select selected value undefined

jqueryhtml

提问by Thug

i have problem with this line var value = $("#opstina option:selected").val();, but only in chrome i get that value is undefined. In FireFox, and IE, works fine, only in chrome i get this warning. Does someone know why? Html code is

我对这条线有问题 var value = $("#opstina option:selected").val();,但只有在 chrome 中,我才发现该值未定义。在 FireFox 和 IE 中,工作正常,只有在 chrome 中我才会收到此警告。有人知道为什么吗?HTML代码是

<select id="opstina" name="opstina">
      <option value="0">Izaberite Mesto</option>
      <option value="1" selected="selected">Novi Sad-grad 1</option>
     <option value="2">Beograd-Stari Grad</option>
</select>

This is my jquery code

这是我的 jquery 代码

if ($("#drzava option:selected").length) {
                    $("select#opstina").attr("disabled","disabled");
                    $("select#opstina").html("<option>wait...</option>");
                    var id = $("select#drzava option:selected").attr('value');
                        $.post("select_opstina.php", {id:id}, function(data){
                        $("select#opstina").removeAttr("disabled");
                         alert( "html koji bi trebao biti upisan u select:\n\n" + data );
                        $("select#opstina").html(data);
            });
                        if ($("#opstina option:selected").length) {
                        alert('Do?ao sam pred petlju');
                         var value = $("#opstina option:selected")[0].value;
                         alert(value);
                        var sel = $("#opstina").val();
                        if (sel!=0) {
                              alert('U?ao sam u petlju, i selektovano polje za op?tinu je' + sel );
            $("select#mesto").attr("disabled","disabled");
            $("select#mesto").html("<option>wait...</option>");
            var id= $("select#opstina option:selected").attr('value');
            alert(id);
            $.post("select_mesto.php", {id:id}, function(data){
                alert("html koji bi trebao biti upisan u select:\n\n" + data );
                $("select#mesto").removeAttr("disabled");
                $("select#mesto").html(data);
            });
        }
    }

                } 
}

回答by Rakesh Juyal

Following should work in all browser:

以下应该适用于所有浏览器:

$('#drzava').find(":selected").val();   //tested in Chrome, safar, FF.

Also, your drop down is not multi-select, so you can simply use this as well:

此外,您的下拉菜单不是多选的,因此您也可以简单地使用它:

$('#drzava').val(); //For multi-select too this work. In that case it gives you array of values.

回答by Adil

Its working for me, you can also use value with DOM object you can use indexer to convert to DOM object or use get function

它对我有用,您也可以使用 DOM 对象的值,您可以使用索引器转换为 DOM 对象或使用 get 函数

Live Demo

现场演示

$("#opstina option:selected")[0].value;
$("#opstina option:selected").val()

回答by BENARD Patrick

I'm confuse because :

我很困惑,因为:

$('#opstina').on('change', function(){
    alert (  $('#opstina option:selected').val()  );
});

works : http://jsfiddle.net/Vm3qu/

作品:http: //jsfiddle.net/Vm3qu/

and with base selected : http://jsfiddle.net/Vm3qu/1/

并选择基地:http: //jsfiddle.net/Vm3qu/1/

回答by S. S. Rawat

Try this this will help you

试试这个这会帮助你

   $("#opstina").change(function(){
        var value = $(this).val();// Value of selected option
        var Text = $(this).val();// Text of selected option
        alert('Value: '+value);
        alert('Text:' +Text);
    });

Fiddle Here

小提琴 Here

回答by mrana

I ran into exact same issue years later from you :) It will sound weird, but doing this simple change worked for me!! I had to use different values for id and name.
Change select id="opstina" name="opstina"to select id="opstina" name="some other name"

几年后我遇到了完全相同的问题:) 这听起来很奇怪,但做这个简单的改变对我有用!!我不得不为 id 和 name 使用不同的值。
更改select id="opstina" name="opstina"select id="opstina" name="some other name"

After this, your $("#opstina option:selected").val()will work fine.

在此之后,您 $("#opstina option:selected").val()将正常工作。