只有我的else if和else语句得到评估,而我的if语句没有

时间:2018-12-30 19:02:59

标签: javascript jquery

我正在制作2个按钮,一个应该是白色主题,一个应该是黑色主题,这些将被发送到我的数据库。但是,当它应该评估我的if语句并执行花括号内的部分时,会引发错误“未捕获的ReferenceError:未定义数据”。

我已经使用过console.log(),并且一切似乎都正常。我查找了其他与if / else if / else语句不起作用等有关的问题,但是对我来说,这些问题都不有用。

如何创建按钮:

function whiteOrDarkTheme(id, value, text, classColor, tracker) {
let a = ($('<a/>', {
    class: classColor + ' blackOrWhiteThemeBtns',
    id: id,
    value: value
})).text(text).on('click', function() {
    if ($(this).attr('id') === 'darkTheme') {

            $('#whiteTheme').val('NotChosen');
            $(this).val('#282c34');
            console.log($(this).val());
            console.log($('#whiteTheme').val());

    } else if ($(this).attr('id') === 'whiteTheme') {

            $('#darkTheme').val('NotChosen');
            $(this).val('#ffff');
            console.log($(this).val());
            console.log($('#darkTheme').val());
    }
});
return a;
}

我的ajax调用看起来是这样的:

function registerCall() {
$('#signup').submit(function(event) {
    event.preventDefault();
    let loginName = $('#registerInput').val();
    let userName = $('#userName').val();
    let pwd = $('#registerPWD').val();
    let confirmPWD = $('#confirmPWD').val();
    let submit = $('#registerBtn').val();
    let backgroundChooser = $('#backgroundChooser').val();
    let whiteTheme = $('#whiteTheme').val();
    let darkTheme = $('#darkTheme').val();

    if (whiteTheme === 'NotChosen') {
            let data = {
                    loginName: loginName,
                    userName: userName,
                    pwd: pwd,
                    confirmPWD: confirmPWD,
                    theme: darkTheme,
                    submit: submit
                }
        } else if (darkTheme === 'NotChosen') {
             data = {
                loginName: loginName,
                userName: userName,
                pwd: pwd,
                confirmPWD: confirmPWD,
                theme: whiteTheme,
                submit: submit
            }   
        } else {
             data = {
                loginName: loginName,
                userName: userName,
                pwd: pwd,
                confirmPWD: confirmPWD,
                theme: '#ffff',
                submit: submit
            }               
        }
        console.log(data.theme);

    $.ajax({
        url: 'signup.php',
        type: 'POST',
        data: data,
        success: function(response) {

        }
    });
});
}

我碰到的部分:

    if (whiteTheme === 'NotChosen') {
            let data = {
                    loginName: loginName,
                    userName: userName,
                    pwd: pwd,
                    confirmPWD: confirmPWD,
                    theme: darkTheme,
                    submit: submit
                }
        }

2 个答案:

答案 0 :(得分:0)

我认为这是lexical scope的问题。您在第一个data块中定义了变量if,但是在发出请求时在该块之外使用了变量。

 if (whiteTheme === 'NotChosen') {
            let data = {
                    loginName: loginName,
                    userName: userName,
                    pwd: pwd,
                    confirmPWD: confirmPWD,
                    theme: darkTheme,
                    submit: submit
                }
        }

我建议将data的声明移到您的if块上方,例如:

...
let whiteTheme = $('#whiteTheme').val();
let darkTheme = $('#darkTheme').val();
let data;

if (whiteTheme === 'NotChosen') {
    data = { ...

答案 1 :(得分:0)

Michael Camden是对的-与范围有关,我将简化您的代码

function registerCall() {
    $('#signup').submit(function(event) {
        event.preventDefault();
        let loginName = $('#registerInput').val();
        let userName = $('#userName').val();
        let pwd = $('#registerPWD').val();
        let confirmPWD = $('#confirmPWD').val();
        let submit = $('#registerBtn').val();
        let backgroundChooser = $('#backgroundChooser').val();
        let whiteTheme = $('#whiteTheme').val();
        let darkTheme = $('#darkTheme').val();

        let data = {
            loginName: loginName,
            userName: userName,
            pwd: pwd,
            confirmPWD: confirmPWD,
            submit: submit
            theme : '#ffff'
        }

        if (whiteTheme === 'NotChosen') {
            data.theme = darkTheme;

        } else if (darkTheme === 'NotChosen') {
            data.theme = whiteTheme;

        }

        console.log(data.theme);

        $.ajax({
            url: 'signup.php',
            type: 'POST',
            data: data,
            success: function(response) {}

        });

    });

}