给定以下内容:
<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的值。我假设我需要做一些事情来获取当前选择的值...?
您正在将事件处理程序添加到<select>
元素。
因此,$(this)
将是下拉列表本身,而不是选定的<option>
。
您需要找到选中的<option>
,如下所示:
var option = $('option:selected', this).attr('mytag');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句