开发者

jQuery Load image on select option

I'm trying to lo开发者_开发知识库ad different images into a DIV according on the selected, something like this:

<select>
  <option value="mercedes">Car #1</option>
  <option value="ferrari">Car #2</option>
  <option value="fiat">Car #3</option>
</select>

If the Car #1 es option is selected, load mercedes.jpg into <div id="car"></div>

Any ideas??

Thanks so much!!


You can use the .change() event handler, like this:

$("select").change(function() {
  $("#car").html($("<img />", { src: $(this).val() + ".jpg" }));
});

When the value changes, this creates a new <img>, sets its source to NEWVALUE.jpg and uses .html() to set #car's content to that element .html(element) is a shortcut for .empty().append(element).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜