Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';这难道不能用javascript吗?_Javascript_Variables - Fatal编程技术网

为什么不是';这难道不能用javascript吗?

为什么不是';这难道不能用javascript吗?,javascript,variables,Javascript,Variables,我正在使用它来检测表单上的错误 var error = false; if (val === '') { error = true; } if (error = true) { $('#joinForm .submit').click(function(event) { event.preventDefault(); }); } 真的很简单但不起作用,我是不是错过了一些愚蠢的东西?变量错误默认为false 如果发现错误,则为真 如果发现错误为tr

我正在使用它来检测表单上的错误

var error = false;

if (val === '') {

    error = true;
}

if (error = true) {

    $('#joinForm .submit').click(function(event) {
        event.preventDefault();
    });
}
真的很简单但不起作用,我是不是错过了一些愚蠢的东西?变量错误默认为false

如果发现错误,则为真

如果发现错误为true,则会阻止提交表单?

var error=false;
var error = false;

if (val === '') { // <<< This checks type first, then value
                  //     An empty '' variable is not type-equivalent
                  //     to a (boolean) false value
    error = true;
}

if (error = true) { // <<< You're setting a variable here, which means
                    //     means that you're testing if the variable
                    //     assignment itself is successful, you'll get
                    //     a true result in most cases, and except with
                    //     things like while loops, you shouldn't use this
                    //     form.
                    // Maybe you want == (falsy)? Or === (type-checked)?
    $('#joinForm .submit').click(function(event) {
        event.preventDefault();
    });
}
如果(val==''){/
var错误=false;

如果(val==''{/您应该在提交事件处理程序中执行检查:

$('#joinForm').submit(function(event) {

    var error = false;

    if (val === '') {
        error = true;
    }

    if (error) {
        event.preventDefault();
    }
});

您应该在提交事件处理程序中执行检查:

$('#joinForm').submit(function(event) {

    var error = false;

    if (val === '') {
        error = true;
    }

    if (error) {
        event.preventDefault();
    }
});

当他订阅提交点击以防止事件发生时,代码仍然从根本上被破坏,但从未删除它们。@JohnD-请在另一个答案中提供更多信息。对于这样的问题,通常有许多问题需要关注。
;)
当他订阅时,代码仍然从根本上被破坏单击以提交单击以防止事件发生,但决不删除它们。@JohnD-请在其他答案中提供更多信息。对于此类问题,通常有许多问题需要关注。
;)