【发布时间】:2017-03-30 21:40:03
【问题描述】:
我正在开发一个通过 AJAX 请求 fares.json 并使用实时数据填充选择选项的小部件。最终用户在使用小部件控件时应该能够看到票价总更新。
JSON 数据:
{
"info": {
"anytime": "Valid anytime",
"weekday": "Valid Monday through Friday, 4:00 a.m. - 7:00 p.m. On trains arriving or departing 30th Street Station, Suburban and Jefferson Station",
"evening_weekend": "Valid weekdays after 7:00 p.m.; all day Saturday, Sunday and major holidays. On trains arriving or departing 30th Street Station, Suburban and Jefferson Station",
"advance_purchase": "Tickets available for purchase at all SEPTA offices.",
"onboard_purchase": "Tickets available for purchase from a train conductor aboard SEPTA regional rail trains."
},
"zones": [{
"name": "CCP/Zone 1",
"zone": 1,
"fares": [{
"type": "weekday",
"purchase": "advance_purchase",
"trips": 1,
"price": 4.75
}, {
"type": "weekday",
"purchase": "onboard_purchase",
"trips": 1,
"price": 6.00
}, {
"type": "evening_weekend",
"purchase": "advance_purchase",
"trips": 1,
"price": 3.75
}, {
"type": "evening_weekend",
"purchase": "onboard_purchase",
"trips": 1,
"price": 5.00
}, {
"type": "anytime",
"purchase": "advance_purchase",
"trips": 10,
"price": 38.00
}]
}, {
"name": "Zone 2",
"zone": 2,
"fares": [{
"type": "weekday",
"purchase": "advance_purchase",
"trips": 1,
"price": 4.75
}, {
"type": "weekday",
"purchase": "onboard_purchase",
"trips": 1,
"price": 6.00
}, {
"type": "evening_weekend",
"purchase": "advance_purchase",
"trips": 1,
"price": 3.75
}, {
"type": "evening_weekend",
"purchase": "onboard_purchase",
"trips": 1,
"price": 5.00
}, {
"type": "anytime",
"purchase": "advance_purchase",
"trips": 10,
"price": 45.00
}]
}, {
"name": "Zone 3",
"zone": 3,
"fares": [{
"type": "weekday",
"purchase": "advance_purchase",
"trips": 1,
"price": 5.75
}, {
"type": "weekday",
"purchase": "onboard_purchase",
"trips": 1,
"price": 7.00
}, {
"type": "evening_weekend",
"purchase": "advance_purchase",
"trips": 1,
"price": 5.00
}, {
"type": "evening_weekend",
"purchase": "onboard_purchase",
"trips": 1,
"price": 7.00
}, {
"type": "anytime",
"purchase": "advance_purchase",
"trips": 10,
"price": 54.50
}]
}, {
"name": "Zone 4",
"zone": 4,
"fares": [{
"type": "weekday",
"purchase": "advance_purchase",
"trips": 1,
"price": 6.50
}, {
"type": "weekday",
"purchase": "onboard_purchase",
"trips": 1,
"price": 8.00
}, {
"type": "evening_weekend",
"purchase": "advance_purchase",
"trips": 1,
"price": 5.00
}, {
"type": "evening_weekend",
"purchase": "onboard_purchase",
"trips": 1,
"price": 7.00
}, {
"type": "anytime",
"purchase": "advance_purchase",
"trips": 10,
"price": 62.50
}]
}, {
"name": "NJ",
"zone": 5,
"fares": [{
"type": "weekday",
"purchase": "advance_purchase",
"trips": 1,
"price": 9.00
}, {
"type": "weekday",
"purchase": "onboard_purchase",
"trips": 1,
"price": 10.00
}, {
"type": "evening_weekend",
"purchase": "advance_purchase",
"trips": 1,
"price": 9.00
}, {
"type": "evening_weekend",
"purchase": "onboard_purchase",
"trips": 1,
"price": 10.00
}, {
"type": "anytime",
"purchase": "advance_purchase",
"trips": 10,
"price": 80.00
}]
}]}
我的 HTML 对每个应该填充数据的地方都有 id。
HTML:
<select id="zone"></select><select id="time"></select>
<form id="radio" class="widget-container--radio">
<input type="radio" name="purchase" value="advance_purchase"> Station Kiosk<br>
<input type="radio" name="purchase" value="onboard_purchase"> Onboard<br>
</form>
<form><input type="number" name="firstname" class="widget-container--quantity"><br></form>
<div id="fare-total" class="widget-container--fare"></div>
我写了一点 jQuery 来获取 JSON 数据和输出选项。所需的影响是选择选项(名称,类型,购买和跳闸)票价总量的票价将在#票价中显示。我如何做到这一点?
jquery:
var $zone = $('#zone');
var $time = $('#time');
var $radio = $('#radio');
var $fareTotal = $('#fare-total');
$.getJSON('fares.json', function(data) {
$zone.html('');
$time.html('');
$radio.html('');
$fareTotal.html('');
for (i = 0; i < data['zones'].length; i++) {
$zone.append('<option class="' + data['zones'][i]['name'] + '">' + data['zones'][i]['name'] + '</option>');
$time.append('<option>' + data['zones'][i]['fares'][i]['type'] + '</option>');
$fareTotal.append('<h2 class="">$' + data['zones'][i]['fares'][i]['price'] + '</h2>');
}
});
【问题讨论】:
-
将票价数据放入一个全局变量中。然后当用户选择一个选项时,搜索与该选项关联的票价,获取其价格,并将其放入总 DIV。
-
@Barmar 不确定如何编写,尤其是因为它需要依赖于选择的其他选项。你能写一个例子吗?
-
为什么
zones数组和其中的fares数组使用相同的索引?看起来您需要级联菜单:当用户选择不同的区域时,您需要更改票价菜单以获得该区域的票价。 -
@Barmar 是正确的。选择不同的区域和时间选项时,应显示单个票价。 span>