开发者

jQuery - Syntax Error in a Conditional

$(".editAd").change({
    if ($(this).val() == 1)
        {
   开发者_如何学JAVA         $("#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.


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 
        }
    });


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.


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");
        }
    }
});


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜