简体   繁体   中英

how to keep dropdown selected after refresh

I tried to keep retain selected item after reloading the page this is my drop-down list在此处输入图像描述

after refreshed it looks like this在此处输入图像描述

I used this code for the load drop-down list

<select 
     data-bind="value:SelectedOrganisationData,
                options: GridAllOrganisationDataList,
                event: { change: tanantNameSelect } ,
                optionsText:'TenantName',optionsCaption: 'Choose your organisation ...',
                optionsId:'SelectTenantName'"
                class="dropdown">
</select>

function tanantNameSelect() {
        var data = PayrollIntegrationVM.SelectedOrganisationData().TenantName;

        DisplayMessage(data);
    }

using this script I can get the selected value, 在此处输入图像描述

If you need to retain the data after reloading the page, you have to save the selected value to the database and get the value if the user reloads the page.

To me it look like you are most of the way there. You just need to set the SelectedOrganisationName.

 var data = [ {id: 1, name: 'Test1'}, {id: 2, name: 'Test2'}, {id: 3, name: 'Test3'}, {id: 4, name: 'Test4'}, {id: 5, name: 'Test5'} ] function ViewModel(){ var self = this; self.selectedOption = ko.observable(); self.availableOptions = ko.observableArray(data); self.setSelectedOption = function(item){ self.selectedOption(item); } } var viewModel = new ViewModel() ko.applyBindings(viewModel);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <select data-bind="options: availableOptions, optionsText: 'name', optionsId:'id', value: selectedOption"></select> <br/> <h3>Press button to set the selection</h3> <ul data-bind="foreach: availableOptions"> <li><button data-bind="text: name, click: $parent.setSelectedOption"></button></li> </ul> <pre data-bind="text: ko.toJSON($root)"></pre>

The only way too save the data even if you refresh the page is: Save the data you want there in a database. With doing so, when you refresh the page, the data will still be there. Thanks. Hope it helps:)It helped me too.

Another answer is to make an ajax post instead of full post. That way your selected item will remain selected. Ajax does not refresh the page but only parts of the page that needs refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM