【问题标题】:How to get the position of a selected option in a dropdown如何在下拉列表中获取所选选项的位置
【发布时间】:2013-08-16 10:37:54
【问题描述】:

如何使用jquery查找当前选中选项的位置,如果用户选择不同的选项也需要实时更新。

 <select id="visa_type_c" title="" name="visa_type_c">
     <option selected="selected" value="No Visa" label="No Visa">No Visa</option>
     <option value="EU Visa" label="EU Visa">EU Visa</option>
     <option value="Easy Visa" label="Easy Visa">Easy Visa</option>
     <option value="Hard Visa" label="Hard Visa">Hard Visa</option>
 </select>

我看到了其他线程,但它们略有不同,我似乎无法让它适合我。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

可以只使用纯JS:

document.getElementById("visa_type_c").onchange = function() {
    alert(this.selectedIndex);
}

纯 JS 演示:http://jsfiddle.net/Xxqnr/1/

【讨论】:

    【解决方案2】:
    $("select").on("change", function(ev) {
        console.log(ev.target.selectedIndex);
    });
    

    【讨论】:

    • 这或多或少是我所需要的!
    【解决方案3】:
    <a href='javascript:alert($("#visa_type_c option:selected").index())';>click for index</a>
    

    有很多方法可以做到;如果您将此添加到您的页面,它将显示您选择的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      相关资源
      最近更新 更多