我想从具有json数据的文本文件填充我的选择框。 Json数据被放置在person.txt文件中。如果您有更好的方法从外部文件填充选择框,您也可以提出建议。我有大量的选择框被填充。从文本文件填充选择框
var json = {
"1": "Accountancy",
"2": "American Studies",
"3": "Anatomy & Regenerative Biology",
};
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<title>Test</title>
</head>
<body>
<select class="selectpicker" data-style="btn-inverse" data-width="300px">
<script type="text/javascript">
$.getJSON("person.txt", function(json) {
$.each(json, function(key, value) {
$('.selectpicker').append('<option value="' + key + '">' + json[key] + '</option>');
});
});
</script>
</body>
</html>
我已经做到了。但这是行不通的' –
它工作,如果我删除行“$ .getJSON(”person.txt“,函数(json){”并添加外部json文件为““ –
这取决于你如何保存你的文件,为什么它不起作用?你得到了哪个错误? – Ygalbel