鉴于以下情况:
<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');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)