简体   繁体   English

document.ready中的功能不起作用

[英]Functions in document.ready is not working

I think the Jquery syntax IM using isn't correct, but not sure what is wrong. 我认为使用IM的Jquery语法不正确,但不确定是什么错误。 Maybe someone can give me a hand. 也许有人可以帮我一下。 So I have a HTML Login page, where it asks for user and pw. 因此,我有一个HTML登录页面,其中要求用户和密码。 I want to save these 2 variables as session cookie. 我想将这两个变量保存为会话cookie。

Here is the HTML of the login: 这是登录的HTML:

            <FORM NAME="cf">
                <span class="auto-style1">Username:</span> 
                <input type="hidden" id="messageType" name="messageType" value="3">
                <INPUT TYPE="text" id="userName" NAME="userName" size="20" style="width: 180px">
                <br><span class="auto-style1">Password:</span> <INPUT TYPE="password" id="password" NAME="password" size="20" style="width: 180px">
                <span lang="en-ca">&nbsp;&nbsp;&nbsp;&nbsp; </span>
            </FORM>
            <INPUT TYPE="submit" id="btnSubmit" name="btnSubmit" Value="Sign In" style="width: 109px" class="auto-style2">

Here is the Javascript I have written: 这是我写的Javascript:

$(document).ready(function () {
        $("#btnSubmit").click(function () {
            //collect userName and password
            var messageType = $("#messageType").val();
            var userName = $("#userName").val();
            var password = $("#password").val();
            var YouEntered;
            var YouEntered2;
            var cookie_name1 = "username";
            var cookie_name = "password";
            putCookie();
            putCookie2();

            auth(messageType, userName, password);
        });
    });

//Set Cookies username and password
function putCookie() {
if(document.cookie != document.cookie)
{index = document.cookie.indexOf(cookie_name1);}
else
{ index = -1;}

if (index == -1)
{
YouEntered=document.cf.userName.value;
document.cookie=cookie_name1+"="+YouEntered+"; expires=0"; //Expires when Session Ends
}
}

function putCookie2() {
if(document.cookie != document.cookie)
{index = document.cookie.indexOf(cookie_name);}
else
{ index = -1;}

if (index == -1)
{
YouEntered2=document.cf.password.value;
document.cookie=cookie_name+"="+YouEntered2+"; expires=0"; //Expires when Session Ends
}
}

If I use onclick in the HTML to run the putCookie functions, I can save the cookies, but it won't send to the server. 如果我在HTML中使用onclick来运行putCookie函数,则可以保存cookie,但不会将其发送到服务器。

Any help or reason why it doesn't work is appreciated. 任何帮助或原因,它不起作用,不胜感激。

First change your function as 首先将您的功能更改为

function putCookie(cookie_name1) {
    if(document.cookie != your_cookie){
        index = document.cookie.indexOf(cookie_name1);
    }
    else {
        var YouEntered=document.cf.userName.value;
        document.cookie=cookie_name1+"="+YouEntered+"; expires=0"; //Expires when Session Ends
    }
}

and should call the function like 并且应该像

putCookie(cookie_name1);

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

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