Uncaught ReferenceError错误求助:$(this).val()赋值左侧无效问题
Hey there, let's figure out why you're hitting this error and fix it right away.
That "Invalid left-hand side in assignment" message pops up because you're trying to assign a value to the return result of a function—which JavaScript doesn't allow. Let's look at the problematic line specifically:
$(this).val() = parseInt($("#x_gen_income").val());
$(this).val() is a function call that grabs the current value of your element. You can't assign directly to what it returns. Instead, jQuery's val() method is designed to accept a parameter when you want to set an element's value, not just get it.
Here's the fixed version of your code block, with a couple of readability improvements too:
$(this).blur(function(){ // Store parsed values in variables to avoid repeating work const currentInputVal = parseInt($(this).val()); const genIncomeVal = parseInt($("#x_gen_income").val()); if(currentInputVal > 10000000){ alert("WARNING: INCOME IS ABOVE 10,000,000; PLEASE REVIEW THE INPUT"); // Use val() with a parameter to set the value correctly $(this).val(genIncomeVal); $(this).focus(); } else if(currentInputVal === 0){ alert("ERROR: INCOME IS 0, PLEASE REVISE THE INPUT"); $(this).val(''); $(this).focus(); } else if(currentInputVal < 100000){ alert("..."); // Add your remaining alert logic here } });
A couple of quick notes:
- I added variables for the parsed values to clean up the code and avoid running
parseInt()multiple times on the same elements. - Fixed a tiny typo in your first alert text ("10,000,00" → "10,000,000") to match the condition you're checking.
- Switched
==to===for strict equality check (a good practice in JavaScript to avoid unexpected type coercion).
内容的提问来源于stack exchange,提问作者Yuuki

