简体   繁体   English

JavaScript返回值未定义

[英]Javascript return value undefined

I would like to ask how to return variable 'get_cookies' from function 'save_value'. 我想问一下如何从函数'save_value'返回变量'get_cookies'。 Then write its value in mouseleave event. 然后将其值写入mouseleave事件。

Function 'get_cookies' will save change from input. 函数“ get_cookies”将保存输入的更改。 An error is in return i think. 我认为错误是对的。

Sorry for my bad English. 对不起,我的英语不好。

Here is code: 这是代码:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script src="jquery.cookie.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            function save_value(value) {
                var get_cookie = value;
                return get_cookie;
            };

            $('#submit_value').live('click', function() {
                var get_value = $('#get_value').val();
                save_value(get_value);
            });

            $(document).mouseleave(function() {
                var create_cookie = save_value();
                console.log('save: ' + create_cookie);
            });
        });
    </script>
</head>
<body>
    <input type="text" id="get_value" />
    <input type="button" id="submit_value" value="Click" />
    <div></div>
</body>

console log: 控制台日志:

save: undefined

Thx for answer. 谢谢。

the function save_value() is expecting a parameter. 函数save_value()需要一个参数。 Since here 从这里开始

var create_cookie = save_value();

you're not passing a value to the function, it will return undefined , thus the variable create_cookie value is undefined too 您没有将值传递给函数,它将返回undefined ,因此变量create_cookie值也undefined

Your save_value() function requires an input parameter, so just save_value() on its own is undefined. 您的save_value()函数需要输入参数,因此仅save_value()本身是未定义的。

Did you mean to do this? 您是要这样做吗?

$(document).mouseleave(function() {
    var get_value = $('#get_value').val();
    var create_cookie = save_value(get_value);
    console.log('save: ' + create_cookie);
});

It's quite simple: your function function save_value(value) takes the value parameter, assigns it to a variable, and returns that variable. 这很简单:您的函数function save_value(value)接受value参数,将其分配给变量,然后返回该变量。 When you call it without parameters, like you do here: var create_cookie = save_value(); 当您不带参数调用它时,就像在这里一样: var create_cookie = save_value(); , the value argument will be undefined. ,则value参数将是不确定的。 Hence, the function returns undefined. 因此,该函数返回未定义。

Also: using variables called cookie doesn't set a cookie. 另外:使用名为cookie变量不会设置cookie。 For that, you'll have to use localStorage.setItem('cookieName','cookieValue'); 为此,您必须使用localStorage.setItem('cookieName','cookieValue');

Since you are using latest jQuery version, you should not use live() function, since it has been deprecated in jQuery 1.7. 由于您使用的是最新的jQuery版本,因此不应使用live()函数,因为jQuery 1.7中已弃用了该函数。

Anyway, your issue is different. 无论如何,您的问题是不同的。 Your save_value function does nothing. 您的save_value函数不执行任何操作。 Just creating a variable and returning its value. 只需创建一个变量并返回其值即可。 But you do not pass any arguments, so it returns default value: undefined . 但是您没有传递任何参数,因此它返回默认值: undefined

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

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