2
$('#selectSurvey').append('<option value="1" data-img="/path/image.png">'+ Option1 +'</option>');

$('#selectSurvey').on('change',function(e){
    $('#image-here').attr('src',this.data('img')); //wrong
});

如何在更改下拉列表中获取Option1中的数据?

4

2 回答 2

3

尝试option从选择元素中获取所选内容,然后检索数据,

$('#selectSurvey').on('change',function(e){
  $('#image-here').attr('src',$(this).find('option:selected').data('img')); 
});

由于data-img已使用option元素而不是select.

于 2014-07-03T09:42:49.153 回答
0

你可以试试这个: -

$('#selectSurvey').on('change',function(e){
   var img = $(this).find('option:selected').attr('data-img');
   $('#image-here').attr('src',img); 
});

或使用 .data() (如果您的 jQuery 版本较新,即 >= 1.4.3)

$('#selectSurvey').on('change',function(e){
   var img = $(this).find('option:selected').data('img');
   $('#image-here').attr('src',img); 
});
于 2014-07-03T09:50:46.997 回答