在其中有一个默认选择框 span
示例在这里http://jsfiddle.net/pzphbnxb/18/
<span id="all_locations">
<select name="united_kingdom" id="united_kingdom" class="location" >
<option value="blank">Select</option>
<option value="england">England</option>
</select>
</span>
1)单击选择框并更改值。
2)结果显示下一个选择框
3)如果我单击下一个选择框,则想提醒(例如)id
单击的选择框。但是我看到上面选择框的警告。
这是jQuery
$(document).on('change', '.location', function(){
$('#all_locations').append('<select name="loc_in_england" id="loc_in_england" class="location" ><option value="blank">Select</option><option value="london">London</option>');
alert ( $('.location').find(":selected").val() + ' selected val' );
alert( $(".location").attr('id') + ' select id' );
}); //$(document).on('change'
我的意思是,我单击了'.location'
,单击了某些选择框,我必须获得attr('id')
单击类。为什么我得到attr('id')
第一个选择框的类?
我是否需要使用类似的方法.closest
来获取单击的选择框的ID?如果.closest
,那么最接近什么?
用$(this)
代替$('.location')
$(document).on('change', '.location', function(){
// alert('test');
$('#all_locations').append('<select name="loc_in_england" id="loc_in_england" class="location" ><option value="blank">Select</option><option value="london">London</option>');
alert ( $(this).find(":selected").val() + ' selected val' );
alert( $(this).attr('id') + ' select id' );
}); //$(document).on('change'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句