未捕获ReferenceError:在分配中左侧无效 [英] Uncaught ReferenceError: Invalid left-hand side in assignment

查看:324
本文介绍了未捕获ReferenceError:在分配中左侧无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script>
function urlencode(str) {
   return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}
$('input#q').keyup(function(e) {
 if(e.keyCode == 13) {
  if($('input#q').val().length > 2) 
  { 
   $('input#q').val() = urlencode($('input#q').val());
   document.search_form.submit();
  }
 }
});
$('input#search').click(function() {
 if($('input#q').val().length > 2) 
 { 
  $('input#q').val() = urlencode($('input#q').val());
  document.search_form.submit();
 } 
});
</script>

当我点击搜索按钮,我得到以下错误:未捕获ReferenceError:左侧无效在赋值
但是代码实际上是相同的,当我按输入。

when i click the search button i get the following error : "Uncaught ReferenceError: Invalid left-hand side in assignment" But the code is actually the same as when i press "enter". Can someone explain?

推荐答案

您不能为函数的结果分配新值

You can't assign a new value to the result of a function

$('input#q').val() = urlencode($('input#q').val());

改为使用:

$('input#q').val(urlencode($('input#q').val()))

它不能与keypress一起使用 - 也许页面是在同样的js错误发生后提交。

It wouldn't work with the keypress either - maybe the page is simply submitted after the same js error occurs.

这篇关于未捕获ReferenceError:在分配中左侧无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆