简体   繁体   English

jQuery如何保留页面重新加载时动态创建列表的选定选项

[英]jQuery how to keep selected option for dynamically created list on page reload

Got a select list that populates using javascript, each time the page reloads the list is reset. 每次页面重新加载时,都会得到一个使用javascript填充的选择列表。 and the selection selected by the user is lost, however for all other input options such as radio button, checkboxes or text fields no data is lost. 并且用户选择的选择将丢失,但是对于所有其他输入选项(例如单选按钮,复选框或文本字段),不会丢失任何数据。

//example of dynamcially generated options for a dropdown list
<p id="question">How old are you (in years)?</p>
<select id="q1" name="age" style="width:157px;"></select>
<script language="Javascript"> initializeAgeList("Select Your Age",100); </script> 

//example of static options for a radio button set
<p id="question">Are you male or female?</p>
<input type = 'radio' name ='q3' value= 'male' required>Male<BR>
<input type = 'radio' name ='q3' value= 'female'>Female 

Tried using a global var to determine if the list was already set, but that proved not to work 尝试使用全局var来确定列表是否已设置,但事实证明这行不通

 var isAgeSet = false; //global var

 function initializeAgeList(default_selection,limit ){
     if(!isAgeSet){ //checks to see if list has already been set, on page refresh is set to zero
         var i = 0;
         while (i <= limit){ //adds options until limit is reached
              if(i==0){
                  $("#q1").append(new Option(default_selection,i));
              }else{
                  $("#q1").append(new Option(i,i));
              }
              i++;
          }
          isAgeSet = true;
      }
 };

How can I prevent the loss of the user's selected option in a dropdownlist using jQuery? 如何使用jQuery防止用户在下拉列表中选择的选项丢失?

It sounds like you want to persist your data between page postbacks. 听起来您想在页面回发之间保留数据。 You should save your selected data and read it when page loads (if there is saved data). 您应该保存选择的数据,并在页面加载时读取(如果有保存的数据)。 The data can be saved to a server (database / file) or on the client (local storage). 数据可以保存到服务器(数据库/文件)或客户端(本地存储)上。

Hope this is what you needed... 希望这就是您所需要的...

Yaniv 亚尼夫

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

相关问题 选择onchange重新加载页面并保留选定的选项 - Select onchange reload the page and keep the selected option 重新加载页面时保留选定的选项值 - keep selected option value when page reload 在jQuery UI中重新加载页面后如何保持字段选择的值 - How to keep fields selected values after page reload in jquery UI 页面重新加载后保持选定的下拉菜单选项可见 - Keep selected drop down menu option visible after page reload 当选项更改且需要重新加载页面时,如何将其保持选中状态? - How do I keep the option selected when it changes and I need to reload the page? 页面刷新或重新加载后,如何使用户选择的选项显示在屏幕上? - How do I keep the users selected option showing on the screen after a page refresh or reload? 检查是否在页面重新加载上选择了一个选项 - Check if an option is selected on page reload 页面重新加载时持久选择的选项 - Persistent selected option on page reload 如何使用 PHP 或 Jquery 从动态创建的下拉列表中获取所选选项的 ID? - how I can get the ID of the selected option from a dynamically created dropdown using PHP, or Jquery? 如何在下拉菜单中加载所选选项而无需重新加载页面? - How to load selected option in dropdown menu without having to reload the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM