【发布时间】:2011-10-23 14:15:00
【问题描述】:
我似乎无法从在 IE6/IE7 中查看时动态创建的选择选项中获取值。 IE 总是返回 undefined 作为值。
我有一个设置a fiddle,下面是一个示例的完整源代码(如果您尝试在 IE6/7 中使用 fiddle ...呵呵):
<!doctype html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
var json = "blah blah blah";
jQuery(document).ready(function(){
$('#myForm').html('<select id="sg-1" class="setgroup" name="sg-1"><option value="s-1">Something</option><option value="s-2">Another</option><option value="s-3">Third</option><option value="s-4">Fourth</option></select>');
$('.setgroup').live('change',function(){
updateSelected($(this + ':selected').val(), json);
});
});
function updateSelected(value, json){
//do some stuff with the json in my app
$('#selected').html(value + ' was selected');
}
</script>
</head>
<body>
<form id="myForm">
</form>
<p id="selected" style="font-size:20px; color:#f00;"></p>
</body>
</html>
示例使用 live(),但是我也尝试了使用 .delegate() 的变体。这两种方法都适用于除 IE6/7 之外的所有浏览器。我也尝试过使用 click 作为事件。有什么想法吗?
我还尝试了here 提供的解决方案。问题似乎在于 $(this) 没有被正确解释,好像我在 live/change/delegate 中放置了一个警报,它会正确触发。
【问题讨论】:
标签: jquery internet-explorer jquery-selectors