jQuery Jquery从下拉列表中获取所选值的id

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

Jquery to get the id of selected value from dropdown

javascriptjquery

提问by Psl

I have a drop down list that fetches values from db as follows

我有一个下拉列表,它从数据库中获取值如下

$.get('/getJobs', function (jobs) {
        seljobs = jobs;
        var i = 0;
        jobs.forEach(function (n) {
                           alert("job id----"+n.id)// 32,67,45
                           alert("job names----"+n.names)//test1,test2,test3

            html += '<option value="' + i + '">' + n.names + '</option>';
            i++;
        });
        $('#jobSel').html(html);
    });

i have to get the idof selected dropdown value..

我必须得到id选定的下拉值..

ic in my dropdown i have name values test1,test2,test3ans assosiated id's 32,67,45,

ic 在我的下拉列表中我有名称值test1,test2,test3ans assosiatedid's 32,67,45,

while selecting test1 i have to get id 32 and so ans so.How it is possible

在选择 test1 时,我必须获得 id 32 等等。这怎么可能

<tr>
                        <td width="200px">Jobs</td>
                        <td> 
                            <select id="jobSel" class="longcombo"></select>
                        </td>
                    </tr> 

回答by Satinder singh

Try this

尝试这个

on change event

在更改事件

$("#jodSel").on('change',function(){
    var getValue=$(this).val();
    alert(getValue);
  });

Note: In dropdownlistif you want to set id,text relation from your database then, set id as value in option tag, not by adding extra idattribute inside option its not standard paractise though i did both in my answer but i prefer example 1

注意:dropdownlist如果你想从你的数据库中设置 id,文本关系,然后将 id 设置为选项标签中的值,而不是通过在选项中添加额外的id属性它不是标准的paractise 虽然我在我的回答中都做了但我更喜欢示例 1

HTML Markup

HTML 标记

Example 1:
    <select id="example1">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
        <option value="4">four</option>
    </select>
Example 2 :
    <select id="example2">
        <option id="1">one</option>
        <option id="2">two</option>
        <option id="3">three</option>
        <option id="4">four</option>
    </select>

Jquery:

查询:

$("#example1").on('change', function () {
    alert($(this).val());
});

$("#example2").on('change', function () {
    alert($(this).find('option:selected').attr('id'));
});

View Demo : For example 1, example 2

查看演示:例如示例 1、示例 2

Blog Article : Get and Set dropdown list selected value with Jquery

博客文章:使用 Jquery 获取和设置下拉列表选定值

Learn jQuery: Simple and easy jQuery tutorials blog

学习 jQuery:简单易用的 jQuery 教程博客

回答by Arun P Johny

Try the change event and selected selector

尝试更改事件和选定的选择器

$('#jobSel').change(function(){
    var optId = $(this).find('option:selected').attr('id')
})

回答by Ishan Jain

First set a custom attribute into your option for example nameid(you can set non-standardized attribute of an HTML element, it's allowed):

例如,首先在您的选项中设置自定义属性nameid(您可以设置 HTML 元素的非标准化属性,这是允许的):

'<option nameid= "' + n.id + "' value="' + i + '">' + n.names + '</option>'

then you can easily get attribute value using jquery .attr():

那么您可以使用 jquery 轻松获取属性值.attr()

$('option:selected').attr("nameid")

For Example:

例如:

<select id="jobSel" class="longcombo" onchange="GetNameId">
    <option nameid="32" value="1">test1</option>
    <option nameid="67" value="1">test2</option>
    <option nameid="45" value="1">test3</option>    
    </select>    

Jquery:

查询:

function GetNameId(){
   alert($('#jobSel option:selected').attr("nameid"));
}

回答by Viji

If you are trying to get the id, then please update your code like

如果您正在尝试获取 ID,请更新您的代码,例如

  html += '<option id = "' + n.id + "' value="' + i + '">' + n.names + '</option>';

To retrieve id,

要检索 ID,

$('option:selected').attr("id")

To retrieve Value

检索值

$('option:selected').val()

in Javascript

在 JavaScript 中

var e = document.getElementById("jobSel");
var job = e.options[e.selectedIndex].value;