尽管有其他类似的例子,我不能完全让这个每个循环来填充一个选择列表......但我真的很接近!
根据我之前的问题,我使用 PHP 脚本返回一个 json 数组,基于另一个得到部分回答的问题。脚本 getStudyNames.php 返回此 json 数组:
[{"studyindex":"1","studyname":"ADHD"},{"studyindex":"24","studyname":"ADHD_RD"},{"studyindex":"20","studyname":"AFL"},{"studyindex":"42","studyname":"AFRICANERS"},{"studyindex":"2","studyname":"AHD"},{"studyindex":"22","studyname":"AJS"},{"studyindex":"3","studyname":"ASA-FS"},{"studyindex":"4","studyname":"ASIAN"},{"studyindex":"5","studyname":"AUS TWINS"},...]
但是,我似乎不太了解如何解析此数组的语法正确,然后将其添加到选择列表中。
这是我尝试执行此操作的代码:
$.ajax({
url: 'getStudyNames.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
error: function() {
alert('Refresh of study names failed.');
},
success: function(data) //on receipt of reply
{
var $studylist = $('#studylist').empty();
$data.each(data, function(i, record) {
$studylist.append('<option value='.record.studyindex.'>'.html(record.studyname) );
})
}
});
根据上面的输出,我知道 getStudyNames.php 有效;但是为什么这个学习列表不起作用对我来说仍然是一个谜......感谢任何帮助!