【问题标题】:A javascript console unusual error [duplicate]javascript控制台异常错误[重复]
【发布时间】:2017-07-29 15:18:42
【问题描述】:

刚刚创建了一个简单的脚本来尝试做一个条件语句,但我不知道为什么以及我在控制台中收到他的错误是什么原因

Uncaught ReferenceError: Invalid left-hand side in assignment

这是我的代码,请告诉我这有什么问题

$(document).ready(function() {
$("#postcomment").click(function() {
    var name    = $('#name').val();
    var email   = $('#email').val();
    var comment = $('#comment').val();
    var post    = $('#pt').val();
    if(email == null && name == null && comment = null) {
        alert('Fields cannot be empty');
    } else {
        $.ajax({
            type   : "POST",
            url    : "include/get_data.php",
            dataType : "text",
            data   : {name : name, email : email, comment : comment, post : post, command : 'comment'},
            success  : function(data) {
                $("#all").apppend(data.commenting);
                $("#counts").html(data.count);
                document.getElementById('name').value = '';
                document.getElementById('email').value = '';
                document.getElementById('comment').value = '';
            }
        });
    }
});
});

【问题讨论】:

  • 此处无效comment = null。应该是comment == null
  • 试试comment == null
  • 老实说,只是搜索错误消息。在 google.com 上搜索“Invalid left-hand side in assignment”时,重复项是第一个结果...
  • 这里的问题是comment = null 是一个赋值,但这个错误本身不会导致错误。实际导致该错误的原因是您在逻辑&& 旁边使用了赋值,这导致该行被读取为(something && comment) = null,但&& 运算符的结果不是在任务。

标签: javascript jquery


【解决方案1】:

您的if 语句有comment = null,它是一个赋值运算符。这意味着您在这样做时不小心将comment 设置为null。要进行比较,您需要 ===== 相等运算符。

if(email == null && name == null && comment = null) {

应该是

if(email == null && name == null && comment == null) {

【讨论】:

  • 我没有明白你的意思还是没有注意到,因为我没有看到任何区别
  • 天哪,我真是瞎了哈哈,非常感谢您的帮助
【解决方案2】:

如果你有comment = null,把它改成==。

【讨论】:

  • 这在上面的 cmets 中被提到了两次。不要试图从拼写错误中获得代表,请投票以关闭问题作为离题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多