Cannot call method 'indexOf' of undefined

本文介绍了一个常见的JavaScript错误——对undefined值调用方法,并提供了具体的代码示例及解决方案。通过简单的修改确保了即使元素未定义也能避免程序崩溃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js文件中:

var executors_name = $("#executors_name") ;

var executors_name_val = executors_name.val();
if(executors_name_val.indexOf("、") > 0){
executors_name.val(executors_name_val.substring(0,executors_name_val.length-1)) ;

}


报错:在红色一行 Cannot call method 'indexOf' of undefined


原因:<input type="text" name="executors_name"  id="executors_name" />没有这一行,导致executors_name_val 的值为undefined


解决:    var executors_name = $("#executors_name") ;
var executors_name_val = executors_name.val() || ‘’;
if(executors_name_val.indexOf("、") > 0){
executors_name.val(executors_name_val.substring(0,executors_name_val.length-1)) ;

}

我用了方案1,现在报错了:12:42:58.678 跳转登录页成功 at pages/index/index.vue:16 12:43:07.960 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:07.960 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:07.960 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:07.960 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:07.960 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 [JS Framework] Failed to execute the callback function: TypeError: Cannot read property 'left' of null 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.012 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7 12:43:08.013 reportJSException >>>> exception function:__WEEX_CALL_JAVASCRIPT__, exception:JavaScript execute error!Uncaught TypeError: Cannot read property 'left' of null at (app-service.js:1701:40) 12:43:08.013 Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值