javascript 未捕获的异常:无法在初始化之前调用滑块上的方法;试图调用方法“禁用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9445792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:42:17  来源:igfitidea点击:

uncaught exception: cannot call methods on slider prior to initialization; attempted to call method 'disable'

javascriptjqueryjquery-mobile

提问by jini

The following block of code works:

以下代码块有效:

$("#data\[User\]\[notify_one_day_out\]").val("0");
$('#data\[User\]\[notify_one_day_out\]').slider('disable');
$('#data\[User\]\[notify_one_day_out\]').slider('refresh');

$("#data\[User\]\[notify_one_week_out\]").val("0");
$('#data\[User\]\[notify_one_week_out\]').slider('disable');
$('#data\[User\]\[notify_one_week_out\]').slider('refresh');

$("#data\[User\]\[notify_one_month_out\]").val("0");
$('#data\[User\]\[notify_one_month_out\]').slider('disable');
$('#data\[User\]\[notify_one_month_out\]').slider('refresh');

$("#data\[User\]\[notify_on_release_date_change\]").val("0");
$('#data\[User\]\[notify_on_release_date_change\]').slider('disable');
$('#data\[User\]\[notify_on_release_date_change\]').slider('refresh');

$("#data\[User\]\[notify_available_for_purchase\]").val("0");
$('#data\[User\]\[notify_available_for_purchase\]').slider('disable');
$('#data\[User\]\[notify_available_for_purchase\]').slider('refresh');

when I try replacing the above with the following to condense the code:

当我尝试用以下内容替换上述内容以压缩代码时:

      $('[id^="data"]').val('0');
            $('[id^="data"]').slider('disable');
            $('[id^="data"]').slider('refresh'); 

I get:

我得到:

uncaught exception: cannot call methods on slider prior to initialization; attempted to call method 'disable'

回答by Spodym

Recently I had very similar problem and replacing

最近我遇到了非常相似的问题并更换了

$('.sth').slider('disable');

with:

和:

$('.sth').attr('disabled', true);

helped. Maybe this is worth try.

有帮助。也许这值得一试。

回答by Eric

I had this same error in a project I am working on and the fix was to initialize the sliders prior to using any methods, as the console error almost explicitly states.

我在我正在处理的项目中遇到了同样的错误,修复方法是在使用任何方法之前初始化滑块,因为控制台错误几乎明确指出。

See the jQuery Mobile Docs page on sliders. http://jquerymobile.com/test/docs/forms/slider/

请参阅有关滑块的 jQuery Mobile 文档页面。http://jquerymobile.com/test/docs/forms/slider/

Select all your sliders, call the slider() method to initialize, and the bug is zapped.

选择所有滑块,调用 slider() 方法进行初始化,并消除了错误。

// Select all my inputs with the slider class, and initialize   
$('.slider').slider(); 

回答by user508994

Suggest you add inputto your [id^="data"]. You might have divs or non-sliders with id=data-something.

建议您添加input到您的[id^="data"]. 您可能有 id=data- something 的div 或非滑块。

Also try putting the code in the pageinitevent for each page with sliders:

还可以尝试将代码放入pageinit带有滑块的每个页面的事件中:

$('#page1,#page2,.sliderpage').on('pageinit', function() {
   $('input[id^=data]').val('0').slider('disabled', true).slider('refresh'); 
});

回答by Ukuser32

I found that the issue was I had initialized the sliders in an earlier area of the code and the reference to $(myfield).slider()didn't then exist. Its also worth noting that this doesn't work when classes are used as the selector.

我发现问题是我在代码的较早区域初始化了滑块,$(myfield).slider()然后引用不存在。还值得注意的是,当类用作选择器时,这不起作用。

The solution I used was to define

我使用的解决方案是定义

window['#'+$(this).attr('id')] = $('#'+$(this).attr('id')).slider();

window['#'+$(this).attr('id')] = $('#'+$(this).attr('id')).slider();

And then reference later on ...

然后稍后参考...

window['#'+id].slider('enable');(or disable)

window['#'+id].slider('enable');(或禁用)

Hope that helps someone.

希望能帮助某人。