我们有一个大学校园数据库,我正在尝试为每个州创建链接。在我们的网站上,它会是一个被点击的 SVG 地图,但出于演示目的,它只是一个文本链接。我不知道如何接线。
我在一些 json 结果中进行了硬编码,以确保部分代码有效,但我不确定如何制作,所以当点击页面上的链接时,它会将状态传递给 json 请求。
这是演示 HTML
<a href="http://ml.uscm.org/ministries.json?state=WY&active=true&callback=Wyoming">Wyoming</a>
<a href="http://ml.uscm.org/ministries.json?state=WY&active=true&callback=Florida">Florida</a>
<span class="results"></span>
这是JS
$(document).ready(function () {
var jsonp = '[{"city":"Casper","enrollment":"4426","name":"Casper College","state":"WY","tuition":2232,"url":"http://www.campusventures.org","zip":"82601-2458","id":3928},{"city":"Riverton","enrollment":"2316","name":"Central Wyoming College","state":"WY","tuition":2472,"url":"","zip":"82501-1520","id":818},{"city":"Torrington","enrollment":"1965","name":"Eastern Wyoming College","state":"WY","tuition":2376,"url":"http://www.campusventures.org","zip":"82240-1699","id":819},{"city":"Cheyenne","enrollment":"5302","name":"Laramie County Community College","state":"WY","tuition":2640,"url":"","zip":"82007-3299","id":820},{"city":"Sheridan","enrollment":"4296","name":"Northern Wyoming Community College District","state":"WY","tuition":2398,"url":"","zip":"82801-1500","id":5825},{"city":"Laramie","enrollment":"13922","name":"University of Wyoming","state":"WY","tuition":4278,"url":"http://www.cvuw.com","zip":"82071-3434","id":4073},{"city":"Rock Springs","enrollment":"4013","name":"Western Wyoming Community College","state":"WY","tuition":1145,"url":"","zip":"82902-0428","id":823}]';
var results = '';
var obj = $.parseJSON(jsonp);
$.each(obj, function () {
results += "<h3><a href='http://ml.uscm.org/ministries/" + this['id'] + ".json" + "'>" + this['name'] + "</a></h3>";
results += this['city'] + "<br/>";
$('span.results').html(results);
});
});
这是迄今为止我所拥有的一个jsfiddle示例。