目前我在json.file中有几个问题,如何在点击'刷新'按钮时通过RANDOM命令从json.file中检索所有这些问题?到目前为止,我通过for循环检索它们,因此它按顺序显示所有问题。
<script>
var jsonfile='data.json';
var qData=new Array();
$.getJSON(jsonfile, function(data){
for (var i=0;i<data.activity.length;i++)
{
qData[i]=data.activity[i].question;
}
for(var i=0;i<qData.length;i++)
{
append(qData[i],i);
$('#text'+(i+1)).textinput();
$('#submit'+(i+1)).button();
$('#cancel'+(i+1)).button();
}
function append(data,i)
{
$('#list').append('<li><a href=#mypanel'+(i+1)+' data-icon="arrow-l" data-iconpos="notext" data-shadow="false" data-iconshadow="false" class="ui-icon-nodisc" id="list">'+ data + '</a></li>'); //list item
$('#mypanel'+(i+1)).append("<div align='center' style='margin-top:30px;'><font style='font-family:Helvetica, Arial, sans-serif ;color:white;' size='5px' ><b>Question Hint : </b></font></div><br/><label for=text"+(i+1)+" id=paneltitle"+(i+1)+" style='margin-top:10px;text-align:center;color:white;'>"+data+"</label>");//panel item
$('#paneltitle'+(i+1)).append('<input type="text" id=text'+(i+1)+' >');
$('#mypanel'+(i+1)).append('<a href="#header" data-role="button" id=submit'+(i+1)+' data-inline="true" data-rel="close" data-icon="check" style="margin-left:75px;">Submit</a>');
$('#mypanel'+(i+1)).append('<a href=#mypanel'+(i+1)+' data-role="button" id=cancel'+(i+1)+' data-inline="true" data-rel="close" data-icon="delete2" style="margin-left:75px;">Cancel</a>');
}
$('#list').listview('refresh');
});
</script>
<div data-theme="a" data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a id="PageRefresh" data-icon="refresh">Refresh</a></li>
</ul>
</div>
</div>
答案 0 :(得分:0)
您需要自己随机化,获取对象中的总密钥并选择一个。
$.getJSON(jsonfile, function(data) {
var max = data.activity.length;
var min = 0;
var random = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(data.activity[random]);
});
答案 1 :(得分:0)
尝试
while (data.activity.length) {
var index = Math.floor(Math.random() * data.activity.length);
qData.push(data.activity[index]);
data.activity.splice(index, 1)
}
完整代码
var jsonfile = 'data.json';
var qData = [];
$.getJSON(jsonfile, function(data) {
while (data.activity.length) {
var index = Math.floor(Math.random() * data.activity.length);
qData.push(data.activity[index]);
data.activity.splice(index, 1)
}
for (var i = 0; i < qData.length; i++) {
append(qData[i], i);
$('#text' + (i + 1)).textinput();
$('#submit' + (i + 1)).button();
$('#cancel' + (i + 1)).button();
}
function append(data, i) {
$('#list')
.append('<li><a href=#mypanel'
+ (i + 1)
+ ' data-icon="arrow-l" data-iconpos="notext" data-shadow="false" data-iconshadow="false" class="ui-icon-nodisc" id="list">'
+ data + '</a></li>'); // list item
$('#mypanel' + (i + 1))
.append("<div align='center' style='margin-top:30px;'><font style='font-family:Helvetica, Arial, sans-serif ;color:white;' size='5px' ><b>Question Hint : </b></font></div><br/><label for=text"
+ (i + 1)
+ " id=paneltitle"
+ (i + 1)
+ " style='margin-top:10px;text-align:center;color:white;'>"
+ data + "</label>");// panel item
$('#paneltitle' + (i + 1)).append('<input type="text" id=text'
+ (i + 1) + ' >');
$('#mypanel' + (i + 1))
.append('<a href="#header" data-role="button" id=submit'
+ (i + 1)
+ ' data-inline="true" data-rel="close" data-icon="check" style="margin-left:75px;">Submit</a>');
$('#mypanel' + (i + 1))
.append('<a href=#mypanel'
+ (i + 1)
+ ' data-role="button" id=cancel'
+ (i + 1)
+ ' data-inline="true" data-rel="close" data-icon="delete2" style="margin-left:75px;">Cancel</a>');
}
$('#list').listview('refresh');
});
演示:Fiddle