2

如何根据下拉选择动态更新 img URL 的结尾部分?for example when blue is selected the filename before .jog would get filled in with the word blue. 它正在使用 css,我无法将任何 id 添加到:

<img src="www.mysite.com/blue.jpg">

<select>
  <option>green</option>
  <option>blue</option>
</select>
4

3 回答 3

2
$(document).ready(function() {
    $('select').change(function(){
       var src = $(':selected', this).text()
       $('img').attr('src', location.hostname + "/" + src + '.jpg');
    });
});
于 2012-09-01T14:54:25.627 回答
0

尝试这个:

$(function() {
  var $img = $('img'), $select = $('select').on('change keyup', function() {
    $img.attr('src','http://www.mysite.com/' + $select.find(':selected').html() + '.jpg')'
  });
});
于 2012-09-01T14:51:43.117 回答
0
$(function () {
  $('select').change(function () {
    $(this).parent().attr('src', location.hostname + "/" + this.val() + '.jpg');
  });
});
于 2012-09-01T15:26:31.487 回答