有一个简单的民意调查示例使用JSON,但它不起作用

时间:2015-02-15 15:42:09

标签: jquery html ajax json

使用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>

1 个答案:

答案 0 :(得分:0)

这是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;
}

工作Fiddle