use*_*700 11 jquery json drop-down-menu
我已经设法使用json对象填充我的下拉菜单,该工作正常.目前我正在尝试根据从下拉列表中选择的选项显示隐藏div中的图像.由于json对象填充了下拉列表,我将如何检索图像数据.
HTML
<form>
<fieldset id="autoHeight">
<legend>pod</legend>
<h2>Cars</h2>
<select name="drop_down" id="dropDownCars">
<option value="None" selected="Selected">Select type</option>
</select>
</fieldset>
</form>
<div id="showBmw" class="hidden">
<a href="http://cdn.iphoneincanada.ca/wp-content/uploads/2012/08/white-bmw.jpg"></a>
</div>
Run Code Online (Sandbox Code Playgroud)
JSON文件
{
Cars: [{
"CarType": "BMW",
"carID": "bmw123"
}, {
"CarType": "mercedes",
"carID": "merc123"
}, {
"CarType": "volvo",
"carID": "vol123r"
}, {
"CarType": "ford",
"carID": "ford123"
}]
}
Run Code Online (Sandbox Code Playgroud)
这是我使用jquery填充下拉菜单的方法.
$(document).ready(function() {
$.getJSON("../cars.json", function(obj) {
$.each(obj.cars, function(key, value) {
$("#dropDownCars").append("<option>" + value.carsName + "</option>");
});
});
});
Run Code Online (Sandbox Code Playgroud)
在jfiddle的任何工作示例,将非常感谢!谢谢.
iJa*_*ade 15
尝试这段代码.... jsfiddle 它解释了根据选定的下拉ID将代码放到哪里选择图像的位置
Html代码
<select id="dropDownDest">
</select>
Run Code Online (Sandbox Code Playgroud)
jQuery document.ready代码
var a = {
Cars: [{
"CarType": "BMW",
"carID": "bmw123"
}, {
"CarType": "mercedes",
"carID": "merc123"
}, {
"CarType": "volvo",
"carID": "vol123r"
}, {
"CarType": "ford",
"carID": "ford123"
}]
};
$.each(a.Cars, function (key, value) {
$("#dropDownDest").append($('<option></option>').val(value.carID).html(value.CarType));
});
$('#dropDownDest').change(function () {
alert($(this).val());
//Code to select image based on selected car id
});
Run Code Online (Sandbox Code Playgroud)
$('#dropDownDest').on('change', function() {
if ($(this).val() == 'vol123r') {
$('#imghide').removeClass('hide');
} else {
$('#imghide').addClass('hide');
}
});
Run Code Online (Sandbox Code Playgroud)