jQuery - 条件中的语法错误

发布于 2024-12-05 23:18:01 字数 465 浏览 0 评论 0原文

$(".editAd").change({
    if ($(this).val() == 1)
        {
            $("#submitBtn").val("Next Step");
        }
        else
        {
            $("#submitBtn").val("Submit Changes");
        }
    });

该代码的第二行有语法错误。缺少:房产 ID 后。我发誓这是在条件中正确使用 val() ,但我想我可能是错的......

我知道这是一个非常简单的修复,但我在几分钟的研究中找不到任何资源,我希望 SO 的用户可以帮助我。 :)

谢谢你!

编辑:开发人员总是会犯一个愚蠢的错误,让他们困扰一段时间......始终检查整个代码是否有错误,因为开发人员工具实际上可能无法拾取正确的错误行,就像在本例中一样。

$(".editAd").change({
    if ($(this).val() == 1)
        {
            $("#submitBtn").val("Next Step");
        }
        else
        {
            $("#submitBtn").val("Submit Changes");
        }
    });

There is a syntax error on the second line of this code. Missing : after property ID. I swore this was the proper use of val() in a conditional, but I suppose I could be wrong...

I know this is a pretty simple fix, but I can't find any resources off of a few minutes of research, and I was hoping the users of SO could help me out. :)

Thank you!

EDIT: There's always a silly error that developers make that stumps them for a period of time... Always review the ENTIRETY of your code for errors because the developer tools may not actually pick up the proper line of the error, as in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2024-12-12 23:18:01

您忘记添加 function(){}

$(".editAd").change(function(){
    if ($(this).val() == 1)
        {
            $("#submitBtn").val() = "Next Step";
            console.log($("#submitBtn").val());
        }
        else
        {
            (element.attr("name") == "submitBtn").val() = "Submit Changes"; // no idea  what this is but it doesn't look like it will work 
        }
    });

You forgot to include function(){}

$(".editAd").change(function(){
    if ($(this).val() == 1)
        {
            $("#submitBtn").val() = "Next Step";
            console.log($("#submitBtn").val());
        }
        else
        {
            (element.attr("name") == "submitBtn").val() = "Submit Changes"; // no idea  what this is but it doesn't look like it will work 
        }
    });
冰之心 2024-12-12 23:18:01

val() 是一个函数执行。

要设置值,请执行类似

$('selector').val('set me');

而不是

$('selector').val() = 'set me';< /code>

从这里开始。其他事情可能会澄清。

val() is a function execution.

to set a value do something like

$('selector').val('set me');

not

$('selector').val() = 'set me';

start there. Other things might clear up.

冷血 2024-12-12 23:18:01

您的代码存在很多问题。首先,您所说的错误的原因是缺少 function 关键字。这是必需的,因为 change 方法采用函数作为参数。

接下来,您要为 val 方法分配一个值,这是不正确的 ($("#submitBtn").val() = "Next Step";)。您需要将字符串作为参数传递。

第三,您的 else 块非常混乱。我不确定你想在那里做什么,但它会评估为类似 true.val() = "Submit Changes"; 这是完全错误的。这是对函数的另一个赋值,并且您尝试在布尔值而不是 jQuery 对象上调用该函数。

因此,基于所有这些,我认为这就是您想要做的事情:

$(".editAd").change(function(){
    if ($(this).val() == 1) {
        $("#submitBtn").val("Next Step");
        console.log($("#submitBtn").val());
    }
    else {
        if($(element).attr("name") == "submitBtn") {
            $(element).val("Submit Changes");
        }
    }
});

There are numerous problems with your code. Firstly, the cause of the error you are talking about, is the missing function keyword. That's required as the change method takes a function as an argument.

Next, you're assigning a value to the val method, which is incorrect ($("#submitBtn").val() = "Next Step";). You need to pass the string in as an argument.

Third, your else block is very confusing. I'm not sure what you were trying to do there, but it will evaluate to something like true.val() = "Submit Changes"; which is completely wrong. It's another assignment to a function, and you're trying to call that function on a boolean value, not a jQuery object.

So, based on all that, this is what I think you were trying to do:

$(".editAd").change(function(){
    if ($(this).val() == 1) {
        $("#submitBtn").val("Next Step");
        console.log($("#submitBtn").val());
    }
    else {
        if($(element).attr("name") == "submitBtn") {
            $(element).val("Submit Changes");
        }
    }
});
烟若柳尘 2024-12-12 23:18:01

属性 ID 后缺少:
这是由于条件之前缺少 function () 标识符造成的。因为它被括在 {} 中,所以 javascript 解析器认为您正在传递一个带有 if 的对象文字作为属性标识符。它正在寻找后面的冒号来标识该财产的价值。


Not sure what you are trying to do on this line:

(element.attr("name") == "submitBtn").val() = "Submit Changes";

但看起来您正在尝试对布尔值调用 val ,这是无效的。您还将一个字符串分配给 val() 的输出,但这是行不通的。

为了给元素赋值,jQuery 的 val 采用一个参数:

element.val("Submit Changes");

Missing : after property ID
This is caused by the lack of a function () identifier before the conditional. Because it was bracketed {} the javascript parser thought you were passing an object literal with if as a property identifier. It was looking for a colon after it to identify the value of the property.


Not sure what you are trying to do on this line:

(element.attr("name") == "submitBtn").val() = "Submit Changes";

But it looks like you are trying to call val on a boolean value, which is not valid. You are also assigning a string to the output of val() which won't work.

To assign a value to an element, jQuery's val takes one parameter:

element.val("Submit Changes");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文