简体   繁体   English

页面刷新后保留两个相关下拉列表中的值

[英]Retain Values in two related Dropdown List after Page refresh

I have two dropdown lists where on changing the value in the first dropdown, the second dropdown is appended with the values based on the value selected in the first dropdown.我有两个下拉列表,其中在更改第一个下拉列表中的值时,第二个下拉列表附加了基于第一个下拉列表中选择的值的值。

My code is:我的代码是:

 $('#orgdropdown').on('change', function() { var selectVal = this.value; switch (selectVal) { case 'Test Company 1': $("#datasources option[value='test']").remove(); $('#datasources').append(`<option value="Test1">TEST1</option>`); $('#datasources').append(`<option value="Test2">Test2</option>`); break; case 'Test Company 2': // console.log('Test Company 1'); $("#datasources option[value='Test1']").remove(); $("#datasources option[value='Test2']").remove(); $('#datasources').append(`<option value="test3">Test3 Data source</option>`); break; default: $("#datasources option[value='Test1']").remove(); $("#datasources option[value='Test2']").remove(); $("#datasources option[value='Test3']").remove(); } }); $('#datasources').on('change', function() { window.location = 'https://location' + this.value; });
 <select class="orgdropdown" id="orgdropdown" name="organization"> <option value="Test Company 1">Test `Company` 1</option> <option value="Test Company 2">Test Company 2</option> </select> <select class="datadropdown" id="datasources" name="data"> <option value="https://location" selected="">Manage Organization Data Source</option> </select>

Could anyone please suggest me how to retain the selected values in both dropdowns after page refresh?任何人都可以建议我如何在页面刷新后保留两个下拉列表中的选定值?

Use localStorage.使用本地存储。

SO Stacksnippets do not allow them, but try this on your server所以 Stacksnippets 不允许他们,但在你的服务器上试试这个

$('#orgdropdown').on('change', function() {
  var selectVal = this.value;
  localStorage.setItem("orgdropdown",selectVal);
  switch (selectVal) {
    case 'Test Company 1':

      $("#datasources option[value='test']").remove();
      $('#datasources').append(`<option value="Test1">TEST1</option>`);
      $('#datasources').append(`<option value="Test2">Test2</option>`);


      break;
    case 'Test Company 2':
      // console.log('Test Company 1');
      $("#datasources option[value='Test1']").remove();
      $("#datasources option[value='Test2']").remove();
      $('#datasources').append(`<option value="test3">Test3 Data source</option>`);
      break;
    default:
      $("#datasources option[value='Test1']").remove();
      $("#datasources option[value='Test2']").remove();
      $("#datasources option[value='Test3']").remove();


  }

});

$('#datasources').on('change', function() {
  window.location = 'https://location' + this.value;

});
$(function() {
  const orgdropdownval = localStorage.getItem("orgdropdown");
  if (orgdropdownval) {
    $('#orgdropdown').val(orgdropdownval)
    $('#orgdropdown').trigger("change");
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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