问题描述
获取此消息:
Error: cannot call methods on checkboxradio prior to initialization;
attempted to call method 'refresh'
我遵循了在此处找到的演示:丑陋的杂种
i followed the demo found here: Ugly Mongrel
这是我的JS的片段:
function(){
var $pbNum = $('<input checked="checked" id="component-pbnum-'+ _val+
'" name="component-pbnum[]'+
'" type="checkbox" value="'+ _val+ '" data-mini="true">'+
'<label for="component-pbnum-'+ _val+ '">'+ _val+ '</label>'),
$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(在这种情况下为checkbox),但是需要提供在单击/点击时将其删除的功能.
i need to add checkboxradio (checkboxes in this case) dynamically, but provide the ability to remove them when they're click/tapped.
实际上有限的jQM与jQUI相比显得有些令人沮丧.没有这些小部件的(已记录)销毁"方法?反正...
It's actually kinda frustrating how limited jQM seems to be compared to jQUI. No (documented) "destroy" method for these widgets? Anyway...
将复选框添加到我的ControlGroup中就好了.当我单击以在完整的浏览器中将其删除时,它们将被删除,但会引发异常.到目前为止,我发现的所有内容都与以下内容类似: https://stackoverflow.com/a/15180600/258598
Checkboxes add to my ControlGroup just fine. When i click to remove them in a full browser, they are removed, but an exception is thrown. Everything i've found so far answers with something similar to this:https://stackoverflow.com/a/15180600/258598
这就是我要寻找的内容: http://goo.gl/mVOdo
Here's what i looked for: http://goo.gl/mVOdo
我不明白.为什么JS删除小部件时JS会引发有关小部件的错误-尤其是当插入时它已初始化时?
i don't get it. Why is JS throwing an error about the widget when i'm removing it - and especially when it WAS initialized when it was inserted?
修复了$ pbNum HTML字符串中的一个小错字
Fixed a small typo in the $pbNum HTML string
推荐答案
我浏览了jQuery Mobile JS文件,发现当change
触发时,jQM将类添加/删除到复选框,然后调用refresh
方法.checkboxradio('refresh')
增强标记.
I went through jQuery Mobile JS file and found out that when change
triggers, jQM adds/removes classes to the checkbox and then call refresh
method .checkboxradio('refresh')
to enhance the markup.
.remove()
似乎是在refresh
发生之前发生的,因此,如果将删除过程延迟1毫秒,则refresh
会在删除复选框之前.
It seems that .remove()
occurs before refresh
takes place, so if you delay the removal process by 1ms, refresh
will before the checkbox is removed.
值得一提的是,stopImmediatePropagation()
并没有帮助您停止错误消息.
It's worth mentioning that stopImmediatePropagation()
didn't help stopping the error message.
代码
$(document).on('change', '.ui-checkbox', function () {
var box = $(this);
setTimeout(function () {
box.remove();
$('#group').controlgroup().trigger('create');
}, 1);
});
这篇关于删除checkboxradio时jQueryMobile未捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!