如上图所示,我需要根据在第一个选择器(区域)中所做的选择来更新第二个选择器(产品)的选择。
下面是在Json文件的帮助下我用来完成这项工作的JQuery代码(请参见脚本下方):
<script>
$(document).ready(function(){
$("#first-choice").change(function() {
var $dropdown = $(this);
$.getJSON("mysite/App/templates/data.json", function(data) {
alert("Success");
var key = $dropdown.val();
var vals = [];
switch(key) {
case 'APAC':
vals = data.APAW.split(",");
break;
case 'MEA':
vals = data.ME.split(",");
break;
case 'base':
vals = ['Please choose from above'];
}
var $secondChoice = $("#second-choice");
$secondChoice.empty();
$.each(vals, function(index, value) {
$secondChoice.append("<option>" + value + "</option>");
});
});
});
};
</script>
Json文件内容:{“ ER”:“ 12”,“ FE”:“ few,SL”}
以下是与选择器相关的HTML文件:
<h1>Region</h1>
<select id="first-choice">
<option value="base">Please Select</option>
<option value="EE">E</option>
<option value="E">E</option>
</select>
<br>
<h1>Product</h1>
<select id="second-choice">
<option>Please choose from above</option>
</select>
答案 0 :(得分:0)
您只需在getJson中使用文件名
$product_price = $rate_margin * $purchase_price;