1

我在更新 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);
});
4

1 回答 1

1

Don't use .pageSelect as selector, because jQM will create an additional element with this style. When you do ´select = $.mobile.activePage.find('.pageSelect'); select.html(options.html); select.val(options.value);´

you are inserting html code in two elements, the select and the rendered object, and because of that, you lose styling.

Try this: $.mobile.activePage.find('select[class=pageSelect]').html(options.html).select('refresh');

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

Also, if you are starting with jQM, consider moving to v1.4.

于 2014-10-08T09:42:17.953 回答