简体   繁体   English

jQuery动态函数和变量

[英]jQuery Dynamic Function and Variable

I am trying to create a function that handles the 'keyup' event for several input fields and passes the input value to a php script. 我正在尝试创建一个函数来处理多个输入字段的“ keyup”事件,并将输入值传递给php脚本。 Here's the code I have so far 这是我到目前为止的代码

$(document).ready(function () {

    $("#email").keyup(function () {

        val = $("input#email").val();
        what = 'email';

        aFunction(val, what);

    });

});

function aFunction(val, what) {

    var dataString = what + '=' + val;
    var error = "email_check";

    $.post("key.php", dataString, function (data) {
        //if (data.[error] == 'invalid'){
        if (data.email_check == 'invalid') {
            $("#ppp").html('error');
        } else {
            $("#ppp").html('good to go');
        }
    }, "json");

    //return false; 
}

When I uncomment 当我取消评论

//if (data.[error] == 'invalid'){

and comment out 并注释掉

if (data.email_check == 'invalid'){

My the script doesnt execute and js file doesn't load into the firebug script console - I assume means there's an error because when I undo that and refresh I can view it. 我的脚本未执行,并且js文件未加载到firebug脚本控制台中-我认为这意味着存在错误,因为当我撤消该操作并刷新后可以查看它。 I've tried added single and double quotes to the variable. 我试过在变量中添加单引号和双引号。 Also, it would be helpful if there was a way to see what the is error is, but I don't know how to do that. 另外,如果有一种方法可以查看错误是什么,那将很有帮助,但我不知道该怎么做。

这里的主要问题是,您应该使用点符号(“ data.error”)或数组符号(“ data ['error']”),但不能同时使用两者(“ data。['error']”)。

Javascript does not support braces in identifiers. Javascript不支持标识符中的花括号。

If the key is actually just error , you can write if (data.error == 'invalid') . 如果键实际上只是error ,则可以编写if (data.error == 'invalid')
If it is [error] , you'll need to write if (data['[error]'] == 'invalid )`. 如果为[error] ,则需要编写if (data['[error]'] == 'invalid )`。

To see syntax errors, go to Firebug's Console tab. 要查看语法错误,请转到Firebug的“控制台”标签。

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

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