【问题标题】:Error calling refresh() on a jQuery Mobile select在 jQuery Mobile 选择上调用 refresh() 时出错
【发布时间】:2023-03-03 03:23:01
【问题描述】:

我在更新 jQuery Mobile 选择时遇到问题。根据需要,我想更改内容并更新所选值。

这是小提琴(下面的代码): http://jsfiddle.net/cjindustries/0cmg8vvt/2/

我在控制台中收到此错误(以及未更新的选择): 未捕获的错误:无法在初始化之前调用选择菜单上的方法;试图调用方法“刷新”

我在这里看到过类似的事情,但我找到的答案都没有解决我的问题。 我们正在使用 jQueryMobile 1.3.1 和 jQuery 1.9.1。

任何帮助将不胜感激:)

谢谢, 克里斯。

代码...

<div data-role="page" id="p1">
    <div data-role="content">
        <h1>Page 1</h1>
        <select class="pageSelect"></select>
        <a href="#" id="update-select-1" data-role="button">Update Select Menu</a>  
    </div>
</div>

function generateOptions() {

    var numbers = [],
        html = "";

    for (var i=0; i<100; i++) {
        var v = Math.random();
        numbers.push(v);
        html = html + '<option value="' + v + '">' + v + '</option>';
    };

    return {
        html: html,
        value: numbers[5]
    };
};

$(document).on('click', '#update-select-1', function() {
    var options = generateOptions(),
        select = $.mobile.activePage.find('.pageSelect');

    select.html(options.html);
    select.val(options.value);

    // This updates the select but I lose styling
    // select.selectmenu().selectmenu('refresh', true); 

    // This fails
    select.selectmenu('refresh', true);
});

【问题讨论】:

    标签: javascript jquery html jquery-mobile jquery-mobile-select


    【解决方案1】:

    不要使用.pageSelect 作为选择器,因为jQM 会创建一个具有这种样式的附加元素。当你这样做 'select = $.mobile.activePage.find('.pageSelect'); select.html(options.html); select.val(options.value);'

    您在两个元素中插入 html 代码,选择和呈现的对象,因此,您失去了样式。

    试试这个: $.mobile.activePage.find('select[class=pageSelect]').html(options.html).select('refresh');

    http://jsfiddle.net/cjindustries/0cmg8vvt/

    另外,如果您从 jQM 开始,请考虑迁移到 v1.4。

    【讨论】:

    • 谢谢,这很好用。我们使用 1.3.1 已经有一段时间了,现在卡住了。
    猜你喜欢
    • 2013-11-16
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多