【问题标题】:get id attribute of selected items in drop down box获取下拉框中所选项目的 id 属性
【发布时间】:2011-09-06 20:10:50
【问题描述】:

我有许多名为 emp 的下拉框。所以在下面的代码中,我试图打印框的 id,但有些打印空的我在这里做错了什么

  var selected_emp = $('select[name="emp"]');
     selected_emp.children('option:selected').each(function() {
        if($(this).val() != '' && $(this).val() != null)
        {
           alert($(this).attr('id'));
        }
     });

【问题讨论】:

  • 你能把你的html代码的sn-p也贴出来吗?
  • 检查选项标签上是否设置了 id,因为相同的代码对我有用

标签: javascript jquery jquery-selectors jquery-validate


【解决方案1】:

您的选项元素是否具有id 属性?通常,option 元素只有一个值和包含的文本。您可以执行以下操作来打印框的 id 和框中的选定值:

 var selected_emp = $('select[name="emp"]');
 selected_emp.children('option:selected').each(function() {
    if($(this).val() != '' && $(this).val() != null)
    {
       alert($(this).parent().attr('id') + ":" + $(this).val());
    }
 });

或者,只遍历选择框本身:

$('select[name="emp"]').each(function() {
    alert(this.Id);
}

【讨论】:

    【解决方案2】:

    它对我有用:http://jsfiddle.net/zdU9K/

    或者您希望它在选择框更改时发出警报?

    【讨论】:

    • 您好,我有许多名称为 emp 的选择框,
    • 好的,所以你想返回所有名称为emp的选择框的id
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2011-09-06
    • 2023-03-29
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多