我确信,以前曾问过一个类似的问题,我会问一问一时,我可以回答我的特殊情况,但 t没有uck。 基本上,我有一个网络应用程序,像一个时间管理系统一样,为用户服务,使他们能够看到、增加和修改他们在工作时所花的时间间隔。 目前,我有一个简单的箱子——通过美国宇宙航空研究开发机构向服务器发出呼吁——用户可以从中选择姓名。 一俟选定其名称(或就此选择任何其他名称),该网页就向该服务器发出另一封呼,要求其装载所有ug子和资料。
I have been asked to save the last user selected in this select box to be saved as a cookie so that each time the page is loaded, it will automatically load in the information and bugs of that previously-selected user. Yes, I know this isn t quite as protected and streamlined as a full-fledged login authentication system, but it s what I ve been asked to do for the time being. This is what I ve been trying thus far:
HTML
<!DOCTYPE HTML>
<html>
<head>
<title>Lighthouse</title>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.cookie.min.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>
<script type="text/javascript">
$(document).ready(function() {
Script();
});
</script>
</head>
<body>
<p>
<label for="users">Select Your Name: </label>
<select id="userSelect" name="user" onChange="javascript:Update();">
<option value="561">Jon Smith</option>
<option value="482">Bill Murray</option>
<option value="711">Luke Skywalker</option>
<option value="241">Harry Potter</option>
</select>
</p>
</body>
</html>
JavaScript
function Script() {
InitializeUsers();
}
function InitializeUsers() {
var userId = $.cookie("fogbugzId");
if(userId != null) $("#userSelect").val(userId).change();
}
function Update() {
var userId = $("#userSelect").val();
$.cookie("fogbugzId", userId, { expires: 365 });
$.get("servlet?command=getBugs", {
id: userId
}, function(data) {
LoadEverythingAboutThatUser(data);
}
}
Now, I m making use of the jQuery Cookie plugin in order to create and look for the cookie I m setting for the user. Now, I have it set up so that each time the page is loaded, the script first checks for the cookie to see if one is present. If so, it changes the selectbox s value to that of the user ID stored in the cookie. In theory, this is supposed to induce the onChange
function of the select box, Update()
. It may be a bit redundant, but I then set the cookie again to the current ID selected by the box, just in case they chose a different name. So, each time the box changes, it saves the ID. Each time the page loads, it looks for an ID and loads the page with the information of that user.
Unfortunately, it s not working properly. I check Chrome s cookie list and find that the ID is clearly saved properly: However, when I refresh the page, the select box either remains at the starting option or doesn t even load the names into the select box all together. Can anyone either see a problem with what I have here or perhaps have a better recommendation?
Edit:
In addition to updating my code to match the answer I selected, it seemed as though I also needed to place the checking of the cookie and tiggering of the selectbox change within the .complete(function() {})
of the $.get
call. I m not sure if this is right, but I think that since the AJAX request was not immediate, I was having issues with trying to select an option that had not been fully loaded into the selectbox. With this change, I ensure that all of the options are loaded in before I attempt to trigger a change to my desired option.