简体   繁体   English

填充选择框后,jQuery未找到选定的节点

[英]jquery doesn't find a selected node after populating selectbox

    var opt = new Option('test','test', true, true);

$('#country option:selected').selectedIndex

So i've populated the selectbox, and jQuery can't find the selected option using the script below, why is that? 因此,我已经填充了选择框,而jQuery无法使用下面的脚本找到选定的选项,为什么呢?

$('#country').find('option:selected')....

You are trying to call selectedIndex on option instead of select . 您试图在option上调用selectedIndex而不是select You need dom select object to call selectedIndex not the jQuery obejct. 您需要dom select对象才能调用selectedIndex而不是jQuery obejct。 Use the id selector to get the select jQuery object and then use indexer to get the DOM select object to call selectedIndex on it. 使用id选择器获取选择jQuery对象,然后使用索引器获取DOM选择对象以对其调用selectedIndex

$('#country')[0].selectedIndex

or using javascript 或使用javascript

document.getElementById('country').selectedIndex

To get the selected option option:selected and to get the index of selected option you can call index() 要获取所选选项的option:selected并获取所选选项的索引,可以调用index()

$('#country option:selected').index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM