jQuery-从所选选项获取自定义属性


224

给定以下内容:

<select id="location">
    <option value="a" myTag="123">My option</option>
    <option value="b" myTag="456">My other option</option>
</select>

<input type="hidden" id="setMyTag" />

<script>
    $(function() {
        $("#location").change(function(){
            var element = $(this);
            var myTag = element.attr("myTag");

            $('#setMyTag').val(myTag);
        });
    });
</script>

那是行不通的...
当更改选择内容时,我需要怎么做才能将隐藏字段的值更新为myTag的值。我假设我需要做一些事情来获取当前选择的值...?

Answers:


518

您正在将事件处理程序添加到<select>元素。
因此,$(this)将是下拉列表本身,而不是选定的<option>

您需要找到选中的<option>,如下所示:

var option = $('option:selected', this).attr('mytag');

7
让我通宵营业的时间变得如此轻松……+1!
eduncan911

3
请注意,如果您要检索页面加载时选择的选项(不是当前选择的选项),则可以使用$('option [selected]',this)(注:如果在加载页面时未选择任何选项)页面加载,将不返回任何匹配,因此调用attr()将导致未定义。当然,易于测试和处理。)
Jon Kloske 2014年

50

试试这个:

$(function() { 
    $("#location").change(function(){ 
        var element = $(this).find('option:selected'); 
        var myTag = element.attr("myTag"); 

        $('#setMyTag').val(myTag); 
    }); 
}); 

26

那是因为元素是其中具有自定义标记的“选择”而不是“选项”。

试试这个:$("#location option:selected").attr("myTag")

希望这可以帮助。


简短的答案。:thumbsup:
Vicky Thakor '18

9

试试这个:

$("#location").change(function(){
            var element = $("option:selected", this);
            var myTag = element.attr("myTag");

            $('#setMyTag').val(myTag);
        });

在的回调函数中change()this是指select,而不是指selected选项。


8

假设您有很多选择。这可以做到:

$('.selectClass').change(function(){
    var optionSelected = $(this).find('option:selected').attr('optionAtribute');
    alert(optionSelected);//this will show the value of the atribute of that option.
});

7

您非常接近:

var myTag = $(':selected', element).attr("myTag");

4

如果是一种形式,则语法更简单。

var option = $('option:selected').attr('mytag')

...如果有多种形式。

var option = $('select#myform option:selected').attr('mytag')


1

这是带有AJAX调用的整个脚本,目标是在具有多个列表的页面中定位单个列表。在我使用“ id”属性之前,即使我的属性名称为“ ItemKey”,以上所有其他内容均不适用于我。通过使用调试器

Chrome调试

我能够看到所选选项具有属性:带有到JQuery“ id”和值的映射。

<html>
<head>
<script type="text/JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<select id="List1"></select>
<select id="List2">
<option id="40000">List item #1</option>
<option id="27888">List item #2</option>
</select>

<div></div>
</body>
<script type="text/JavaScript">
//get a reference to the select element
$select = $('#List1');

//request the JSON data and parse into the select element
$.ajax({
url: 'list.json',
dataType:'JSON',
success:function(data){

//clear the current content of the select
$select.html('');

//iterate over the data and append a select option
$.each(data.List, function(key, val){
$select.append('<option id="' + val.ItemKey + '">' + val.ItemText + '</option>');
})
},

error:function(){

//if there is an error append a 'none available' option
$select.html('<option id="-1">none available</option>');
}
});

$( "#List1" ).change(function () {
var optionSelected = $('#List1 option:selected').attr('id');
$( "div" ).text( optionSelected );
});
</script>
</html>

这是要创建的JSON文件...

{  
"List":[  
{  
"Sort":1,
"parentID":0,
"ItemKey":100,
"ItemText":"ListItem-#1"
},
{  
"Sort":2,
"parentID":0,
"ItemKey":200,
"ItemText":"ListItem-#2"
},
{  
"Sort":3,
"parentID":0,
"ItemKey":300,
"ItemText":"ListItem-#3"
},
{  
"Sort":4,
"parentID":0,
"ItemKey":400,
"ItemText":"ListItem-#4"
}
]
}

希望能对您有所帮助,谢谢以上所有人对我的帮助。



0
$("body").on('change', '#location', function(e) {

 var option = $('option:selected', this).attr('myTag');

});

0

您也可以尝试使用 data-myTag

<select id="location">
    <option value="a" data-myTag="123">My option</option>
    <option value="b" data-myTag="456">My other option</option>
</select>

<input type="hidden" id="setMyTag" />

<script>
    $(function() {
        $("#location").change(function(){
           var myTag = $('option:selected', this).data("myTag");

           $('#setMyTag').val(myTag);
        });
    });
</script>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.