【问题标题】:There is a simple poll example with using JSON but it doesn't work有一个使用 JSON 的简单民意调查示例,但它不起作用
【发布时间】: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>

【问题讨论】:

标签: jquery html ajax json


【解决方案1】:

这里是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2015-11-23
    • 2016-03-02
    相关资源
    最近更新 更多