(function ($) { $('button').on('click', function () { // remove resultset if this has already been run $('.content ul').remove(); // get selected zip code from selectbox var perfid = $('select option:selected').text(); // make AJAX call $.getJSON('http://myapiurl.com/ws/spm/search/perfid/' +perfid, function (data) { // do all this on success var items = [], $ul; $.each(data, function (key, val) { //iterate through the returned data and build a list items.push('
' + val.case + ' ' + val.userid + '
'); }); // if no items were returned then add a message to that effect if (items.length <1) { items.push('
No Data Found!
'); }
// append list to page $ul = $('
').appendTo('.content'); //append list items to list $ul.append(items); }); }); }(jQuery));
我从2天开始就把头发拉到头上,仍然无法理解.
解决方法:
你的jQuery应该是
$.getJSON('http://myapiurl.com/ws/spm/search/perfid/' + perfid, function (data) { // do all this on success var itemsHtml = "", $.each(data, function (key, val) { //iterate through the returned data and build a list itemsHtml+= '
' + val.case + ' ' + val.userid + '
'; }); // if no items were returned then add a message to that effect if (data.length == 0) { itemsHtml = '
No Data Found!
'; } // append list to page $(".content").append("