最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does jQuery complain about an "invalid left-hand assignment"? - Stack Overflow

programmeradmin3浏览0评论
function auditUpdate(newval) {
    jQuery("#audit").val() = newval;
    jQuery("#auditForm").submit();
}

Why do I get an error where I try to assign newval to the #audit value?

function auditUpdate(newval) {
    jQuery("#audit").val() = newval;
    jQuery("#auditForm").submit();
}

Why do I get an error where I try to assign newval to the #audit value?

Share Improve this question edited Jul 31, 2011 at 23:16 Jeremy Banks - Vive le Canada 130k88 gold badges358 silver badges381 bronze badges asked Jul 31, 2011 at 23:13 FrayceFrayce 511 silver badge2 bronze badges 1
  • Well, the left-hand side of an assignment obviously has to be a reference. An assignment stores the value from the right-hand side into the reference on the left-hand side. The result of calling val() is a string, not a reference, hence the error. – Šime Vidas Commented Jul 31, 2011 at 23:21
Add a ment  | 

3 Answers 3

Reset to default 7

In jQuery you assign a new value with:

jQuery("#audit").val(newval);

val() without a variable works as a getter, not a setter.

jQuery doesn't plain about it. But your JavaScript interpreter does. The line

jQuery("#audit").val() = newval;

is invalid JavaScript syntax. You can't assign a value to the result of a function call. Your code says "call val, get the return value -- and then assign newval to the return value." This makes no sense.

Instead:

function auditUpdate(newval) {
    jQuery("#audit").val(newval);
    jQuery("#auditForm").submit();
}

the right syntax would be:

jQuery("#audit").val(newval);

val is a function, that, when executed, can't be assigned a value like you try to do.

发布评论

评论列表(0)

  1. 暂无评论