您需要創建一個新的img
並將其附加到div
。
的Javascript
$(document).ready(function() {
var pictures = ["https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQYN2T3dktBEDIuwIuosj8ulzlwGod-KabDTy2vBFC-Kht_u4ep","https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQgBLpqp92lc7JQiNszqO_ZP52OymeqShqxqcrlUDcae9UaTTLtfQ", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS99xzsoRm3g0nckrH-SjfgSe8zgeYSCgc23IqtbtocZo3sjZN2gA", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRBLBxc5mMj9WbV9ghIyfpmOfVdt0qyIe21f8HBlqwvVoGgCxQucA"];
$(".nextbutton").button({ disabled: true });
$('.dropdown').change(function() {
var value = $('.dropdown').val();
if (value== 0) {
$(".prevbutton").button({ disabled: true });
} else if (value == 1) {
$(".prevbutton").button({ disabled: true });
$(".nextbutton").button({ disabled: false });
} else if (value == 4) {
$(".nextbutton").button({ disabled: true });
} else {
$(".nextbutton").button({ disabled: false });
$(".prevbutton").button({ disabled: false });
}
//Emptys picture div and appends image
$("#picture").empty();
$("#picture").append($("<img/>",{src: pictures[value-1]}));
});
$(".prevbutton").button({ disabled: true });
});
$("#next").click(function() {
var nextElement = $('#mypicture > option:selected').next('option');
if (nextElement.length > 0) {
$('#mypicture > option:selected').removeAttr('selected').next('option').attr('selected', 'selected');
$('.dropdown').trigger('change');
}
});
$("#prev").click(function() {
var nextElement = $('#mypicture > option:selected').prev('option');
if (nextElement.length > 0) {
$('#mypicture > option:selected').removeAttr('selected').prev('option').attr('selected', 'selected');
$('.dropdown').trigger('change');
}
});
HTML
<select id="mypicture" class="dropdown">
<option value="">Choose picture...</option>
<option value="1">Picture 1</option>
<option value="2">Picture 2</option>
<option value="3">Picture 3</option>
<option value="4">Picture 4</option>
</select>
<hr>
<button id="prev" class="prevbutton">Previous</button>
<button id="next" class="nextbutton">Next</button>
<hr>
<div id="picture">
</div>
工作實例:http://jsfiddle.net/Wdw4z/3/
Rodik,就是這樣,非常感謝。它適用於一個選擇列表,但有兩個列表不起作用。例如,我有兩個列表_選擇狗圖片... _和_選擇貓圖片... _ _選擇狗圖片..._作品,但_選擇貓圖片... _不起作用。在_選擇貓圖片... _我爲圖片1 _value =「101」_和_img101.jpg_設置,但不起作用。你能幫我嗎,[這裏](http://jsfiddle.net/Godfather/UEMCy/)是我的兩個列表的代碼。 – Godfather