使用jQuery mobile和Ajax动态创建垂直无线电组

时间:2012-10-17 15:48:33

标签: jquery jquery-mobile dynamic radio-group

我一直试图解决这个问题几个小时。我甚至在堆栈溢出中查看了其他类似的示例,但它们没有帮助。

success :  function(data) {       
    var subscriptionJSONObject = data; //{"cost":7.0,"duration":30,"id":7},{"cost":120.0,"duration":365,"id":8}
    $('<fieldset data-role="controlgroup" id="paypalFieldset"></fieldset>').appendTo('#paypalContent');                     
    $('<legend id="paypalLegend">Select a subscription:</legend>').appendTo('#paypalFieldset');                     
    $.each(subscriptionJSONObject,function(i) {
          $('<input type="radio" name="radio-choice-'+i+'" id="'+subscriptionJSONObject[i].id+'" value="'+subscriptionJSONObject[i].cost+'"/>').appendTo("#paypalFieldset"); 
          if(subscriptionJSONObject.cost != -1){
          $('<label for="radio-choice-'+i+'">'+subscriptionJSONObject[i].duration+' day subscription for only R'+subscriptionJSONObject[i].cost+'.00</label>').appendTo("#paypalFieldset");     
          } else {
               $('<label for="radio-choice-'+i+'">Free subscription</label>').appendTo("#paypalFieldset");     
          }
    });
    $("#paypalContent").trigger("create");
}

这是我的ajax调用中成功函数的一部分。按钮正在显示,图例也会显示,但它们不会形成带有jquery mobile的垂直无线电组的样式。

1 个答案:

答案 0 :(得分:1)

如果我没弄错的话,应该在页面本身(而不是单个小部件)上进行.trigger("create")调用。这似乎隐含在jQM文档中:

http://jquerymobile.com/test/docs/pages/page-scripting.html

按照这种逻辑,你的最终陈述可能是:

$("#paypalContent").closest("div[data-role=page]").trigger("create");

如果这没有帮助,我也很幸运使用.destroy()后跟.trigger("create"),但我怀疑这被认为是好形式。

祝你好运!