REMOVING checkboxradio时jQueryMobile未捕获exception

得到这条消息:

Error: cannot call methods on checkboxradio prior to initialization; attempted to call method 'refresh' 

我按照这里的演示: 丑陋的杂种

这是我的JS片段:

 function(){ var $pbNum = $(''+ ''), $pbNumChk = $($pbNum[0]).on("change", function(ev){ $pbNum.remove(); // UNCAUGHT EXCEPTION: "cannot call methods on checkboxradio prior // to initialization; attempted to call method 'refresh'" }); $pbnumList.controlgroup("container").append($pbNum); $pbNumChk.checkboxradio(); // Have also tried $pbNumChk.checkboxradio().checkboxradio("refresh") // as hinted by other answers. No difference. } 

我需要动态添加checkboxradio(在这种情况下为复选框),但提供在单击/点击时删除它们的function。

实际上有点令人沮丧的是jQM似乎与jQUI相比有限。 没有(记录)这些小部件的“破坏”方法? 无论如何…

复选框添加到我的ControlGroup就好了。 当我单击以在完整浏览器中删除它们时,它们将被删除,但会引发exception。 到目前为止,我发现的所有内容都回答了类似的内容: https : //stackoverflow.com/a/15180600/258598

这是我寻找的: http : //goo.gl/mVOdo

我不明白。 当我删除它时,为什么JS会抛出关于窗口小部件的错误 – 特别是当它在插入时被初始化时?

[编辑]
修复了$ pbNum HTML字符串中的小错字

我浏览了jQuery Mobile JS文件,发现当change触发器时,jQM会向复选框添加/删除类,然后调用refresh方法.checkboxradio('refresh')来增强标记。

似乎.remove()refresh发生之前发生,因此如果将删除过程延迟1ms,则在删除复选框之前refresh

值得一提的是, stopImmediatePropagation()无助于停止错误消息。

演示

 $(document).on('change', '.ui-checkbox', function () { var box = $(this); setTimeout(function () { box.remove(); $('#group').controlgroup().trigger('create'); }, 1); });