簡體   English   中英

銷毀並重新初始化引導滑塊

[英]Destroy and re-initialize a bootstrap-slider

我正在使用此處找到的 Bootstrap-slider - https://github.com/seiyria/bootstrap-slider ,目前正在使用 v10(盡管我也嘗試過 v11,但結果相同)。 這是我的代碼:

/* For better styling - this uses Bootstrap slider */
$('.bs_sliders').each(function () {
    var id = '#' + $(this).attr('id');

    if (typeof window[id] !== 'undefined') {
        window[id].slider('destroy');
        delete window[id];
    }

    // .on will only work with an ID based selector not class
    window[id] = $(id).slider({
        formatter: function (value) {
            return value;
        },
    }).on('change', function (ev) {
        $(this).closest('li').find('.rangeUpdate').val($(this).val());

        $(this).closest('li').find('.continuing_eval').removeClass('continuing_eval');

        autoSave();
    })
})

基本上,我正在嘗試銷毀並重新初始化 slider(如果存在)。 雖然它是半有效的,但我注意到(例如)我的范圍是 0-100,即使輸入如下,它也只會達到 10:

<input type="text" name="54_slider" value="22" class="bs_sliders elRes" id="54_slider" data-slider-id="54_slider"
    data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="22"
    data-slider-handle="custom"
    data-slider-rangeHighlights='[{"start":25,"end":26,"class":"grey_line"},{"start":50,"end":51,"class":"grey_line"},{"start":75,"end":76,"class":"grey_line"}]' />
<input type="text" value="22" class="rangeUpdate" data-toggle="popover" data-trigger="manual"
    data-content="The permitted value range is 0-100" style="--my-content:'Undefined';" />

此外,它也不支持自定義元素(例如分隔線)。 我怎樣才能使它在重新初始化 bs-slider 時接收到所有自定義元素和屬性?

原來的解決方案是自己銷毀它......我們所做的是克隆原始元素 - 獲取它的值,刪除元素,重新附加克隆,然后在 slider 上設置值。 相當冗長,你會期望“破壞”來做到這一點,但它顯然沒有。

$('.bs_sliders').each(function(){
    var id = $(this).attr('id');

    // Detach and Reattach slider cloning the existing element
    if (typeof my_sliders[id] !== 'undefined'){
        delete my_sliders[id];

        var setVal = $(this).attr('value');

        // Parent of current element for new element
        var our_parent = $(this).parents('.slide_slidecontainers');

        // Rebuild the element
        var new_elem = $(this).clone();

        // Get rid of the actual input and rebuild it
        $(this).remove();

        $(our_parent).find('.slider').remove();

        $(our_parent).append($(new_elem));

        use_elem = $(new_elem);
    // Simply Attach using the existing element
    } else {
        use_elem = $(this);
    }

    my_sliders[id] = $(use_elem).slider();

    if (typeof setVal !== 'undefined'){
        my_sliders[id].slider('setValue',setVal);
    }

    my_sliders[id].on('change',function(ev){
        $(this).closest('li').find('.rangeUpdate').val($(this).val());

        $(this).closest('li').find('.continuing_eval').removeClass('continuing_eval');

        autoSave();
    })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM