Jquery 有一个使用JSON的简单轮询示例,但它没有';行不通
有一个使用JSON的简单轮询示例,但它不起作用。 我不想从外部文件读取JSON。我哪里做错了?有没有人用正确的方法写这个例子。谢谢Jquery 有一个使用JSON的简单轮询示例,但它没有';行不通,jquery,html,ajax,json,Jquery,Html,Ajax,Json,有一个使用JSON的简单轮询示例,但它不起作用。 我不想从外部文件读取JSON。我哪里做错了?有没有人用正确的方法写这个例子。谢谢 { "result":[ { "option":"Lorem Ipsum", "percent":"40" }, { "option":"Dolor sit amet", "percent":"22" } ] } function getData(){ (function(read_data) {
{
"result":[
{
"option":"Lorem Ipsum",
"percent":"40"
},
{
"option":"Dolor sit amet",
"percent":"22"
}
]
}
function getData(){
(function(read_data) {
percent1 = read_data.result[0].percent;
percent2 = read_data.result[1].percent;
$('#pollChoice1').html('%' + percent1);
$('#pollChoice2').html('%' + percent2);
}).done(function (){
$.cookie("choice1", percent1, { expires: 1 });
$.cookie("choice2", percent2, { expires: 1 });
$('#pollChoice1').parents().eq(1).find('.col-line-graph').animate({width: percent1 + '%'});
$('#pollChoice2').parents().eq(1).find('.col-line-graph').animate({width: percent2 + '%'});
});
}
<div class="col-line">
<label>
<span id="pollChoice1"><input type="radio" class="pollCustom" name="pollRadio" value="1"></span><span class="lbl poll-text">Lorem Ipsum</span>
</label>
<div class="col-line-graph"></div>
</div>
<div class="col-line">
<label>
<span id="pollChoice2"><input type="radio" class="pollCustom" name="pollRadio" value="2"></span><span class="lbl poll-text">Dolor Sit Amet</span>
</label>
<div class="col-line-graph"></div>
</div>
{
“结果”:[
{
“选项”:“Lorem Ipsum”,
“百分比”:“40”
},
{
“选项”:“Dolor sit amet”,
“百分比”:“22”
}
]
}
函数getData(){
(功能(读取数据){
percent1=读取数据。结果[0]。百分比;
percent2=读取数据。结果[1]。百分比;
$('#pollChoice1').html('%'+percent1);
$('#pollChoice2').html('%'+percent2);
}).done(函数(){
$.cookie(“choice1”,percent1,{expires:1});
$.cookie(“choice2”,percent2,{expires:1});
$('#pollChoice1').parents().eq(1).find('.col line graph').animate({width:percent1+'%'));
$('#pollChoice2').parents().eq(1).find('.col-line-graph').animate({width:percent2+'%'));
});
}
乱数假文
多洛·希特·阿梅特
以下是jQuery
var read_data = {
"result":[
{
"option":"Lorem Ipsum",
"percent":"40"
},
{
"option":"Dolor sit amet",
"percent":"22"
}
]
}
$(document).ready(function(){
percent1 = read_data.result[0].percent;
percent2 = read_data.result[1].percent;
$('#pollChoice1').html('%' + percent1);
$('#pollChoice2').html('%' + percent2);
$('#pollChoice1').parents().eq(1).find('.col-line-graph').animate({width: percent1 + '%'});
$('#pollChoice2').parents().eq(1).find('.col-line-graph').animate({width: percent2 + '%'});
});
我使用了一些CSS
,否则不会显示div
.col-line-graph
{
height:10px;
background-color:#f00;
}
工作如何调用此函数?JSON从何而来?它是一个harcoded变量还是通过ajax来的?您不需要不必要地使用
(函数(读取数据){
和.done
。