1

クリアされるまで、ブートストラップのポップオーバー内でユーザーのドロップダウン選択を保持する必要があります。選択の変更に基づいて「selected」属性を動的に変更できるようにし、その変更を HTML の select 要素に置き換えました。すべてが機能していると思います(アラート機能で確認しています)。残念ながら、ポップオーバーの「外観」ではありません。はい、それを調べると、アラートで得たものと一致しません。

これが私のフィドルです。ありがとうございました。 http://jsfiddle.net/kDmVq/

$(document).on('shown', "#btnPopover", function () {
    $('select#optionDropdown').select2({
        allowClear: true
    }).change('#optionDropdown', function () {
        theID = $(this).val();
        theSelection = $(this).children('option:selected').text();
        $('#selectedID').text(theID);
        $('#selectedText').text(theSelection);
        $('#optionDropdown option').removeAttr("selected");
        $('option[value=' + theID + ']').attr("selected", "selected");
        optionDropdownRet = $('#optionDropdown').html();
    });
    alert($('#optionDropdown').html());
});

$(document).on('hide', "#btnPopover", function () {
    alert(optionDropdownRet);
    $('options#optionDropdown').replaceWith(optionDropdownRet);
});
4

1 に答える 1

1

私が思いついた解決策は、非表示の div とポップオーバーの間でポップオーバー コンテンツを行ったり来たりすることでした。ブートストラップ ポップオーバー イベントの後にクリック イベント リスナーをポップオーバー ボタンに追加することで、ポップオーバー コンテンツが表示された後に取得し、フォーム要素を反復処理することができました...

$(document).ready(function() {

    $('#pop').popover({ 
        html : true,
        title: 'Popover',
        content: function() {
            html = $('#popover_content').html();
            return html;
        }
    });

    $('#pop').on('click', function (e) {
        initPopover();
    });

});

トリックは、ポップオーバー関数の後にクリック イベント リスナーを追加することでした。そして、「initPopover」で……

function initPopover() {
    var ishidden = true;
    if($('.popover').hasClass('in')) {
        ishidden = false;
    } else {
        ishidden = true;
    }

    if(ishidden == true) {

        var content = $('.popover-content');
        var html = content.html();

        $('#popover_content').html(html);

        $('#popover_content select').each(function(i){
            var sel = $('.popover-content select').eq(i).val();
            $(this).val(sel);
         });

    } else {

        $('.popover-content select').each(function(i){
            var sel = $('#popover_content select').eq(i).val();
            $(this).val(sel);
        })

        $('#popover_content').empty();  

    }

} 

これがフィドルです:http://jsfiddle.net/5NQ84/5/

于 2014-07-17T15:34:26.053 に答える