【发布时间】:2015-02-15 23:57:49
【问题描述】:
有一个使用 JSON 的简单投票示例,但它不起作用。 我不想通过从外部文件读取 JSON 来做到这一点。我在哪里做错了?有没有人为这个例子写正确的方法。谢谢。。
{
"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>
【问题讨论】:
-
你是怎么调用这个函数的? JSON从哪里来?它是一个硬编码的变量还是来自 AJAX
-
你不需要使用
(function(read_data) {和.done。