English 中文(简体)
采用库克群岛语来替代以前的用户
原标题:Using a Cookie to Remember a Previous User

我确信,以前曾问过一个类似的问题,我会问一问一时,我可以回答我的特殊情况,但 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:&nbsp;</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: enter image description here 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.

最佳回答

问题在于<代码>.change();. 该功能应当把更改活动与一项内容联系起来,而不是引起改动。

你们可以做的是使用:

if(userId != null) $("#userSelect").val(userId).trigger( change );

促成触发机制的最佳途径是删除选择的死板,并增加以下代码(文件)。 准备就绪:

$( #userSelect ).change(function() {
    Update();
});

如果你在“上岸”上使用,更新功能应在页数上加以界定,则可能在某些假想中无法使用(jax, fiddle)。 (基本上,在选择之前,你必须确定在html的更新职能。) 此外,最好把所有法典放在“档案”。

rel=“nofollow”>here为 fiddle

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签