动态添加单选按钮后,控制组失去控制权-jQuery Mobile [英] Control group loses control after dynamic add of radio button - jQuery Mobile

查看:67
本文介绍了动态添加单选按钮后,控制组失去控制权-jQuery Mobile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制组(jquery mobile 1.4)在最初动态创建单选组时完美呈现,但是当我向其中添加另一个单选时,按钮样式设置但分开.当我重新加载应用程序时,它们又在一起了.

My control group (jquery mobile 1.4) renders perfectly when dynamically creating the radio group initially, but when I add another radio to it, the buttons are styled, but separated. When I reload the app, they are together again.

function showPlaces() {    
    $('#radio-group').empty();

    for(var i = 0; i < connections.length; i++) {
        $('#radio-group').append('<label><input type="radio" name="places" id="' + i + '" />' + place + '</label>');           
    }

    $('input[type=radio]').checkboxradio().trigger('create');
    $('#radio-group').controlgroup().trigger('create');
}

//该函数在应用启动时调用,并在我添加另一个位置后再次调用

// The function is called when app starts, and again after I add another place

我尝试过的其他一些事情:

Some other things I've tried:

$("#radio-group").controlgroup("refresh");                                          
$('#radio-group').controlgroup('refresh');
$("[data-role=controlgroup]").controlgroup("refresh");
$('input[type=radio]').checkboxradio('refresh');

还在.append之前尝试过.controlgroup('container'),但得到初始化前无法在controlgroup上调用方法".

Also tried .controlgroup('container'), before the .append, but get "cannot call methods on controlgroup prior to initialization".

这是html:

<form>  
    <fieldset data-role="controlgroup" id="radio-group">
        <!-- Dynamically injected radio buttons go here -->
    </fieldset>
</form> 

推荐答案

您应.append()将项目.append()放入 for循环内的.controlgroup("container") ,然后增强单选按钮.checkboxradio().

for(var i = 0; i < connections.length; i++) {
  $('#radio-group')
      .controlgroup("container")
      .append('<label><input type="radio" name="places" id="+i+" />Place</label>');           
}

$("#radio-group")
    .enhanceWithin()
    .controlgroup("refresh");

您甚至不需要使用.checkboxradio(),而是在父容器上使用.enhanceWithin().

You dont even need to use .checkboxradio(), instead use .enhanceWithin() on parent container.

演示

Demo

这篇关于动态添加单选按钮后,控制组失去控制权-jQuery Mobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆