Uncaught TypeError: Cannot call method 'replace' of undefined 的解决
1. js代码
var rip = $("#RSIP").val();
rip = rip.replace(/\r|\n/g,":");
报错 Uncaught TypeError: Cannot call method 'replace' of undefined
2. 代码更改为
var rip = $("#RSIP").val();
console.log(rip)
rip = rip.replace(/\r|\n/g,":");
得到输出为
Undefined editTask:28
Uncaught TypeError: Cannot call method 'replace' of undefined
3.可见 rip为未定义,在html中搜 RSIP,发现
<textarea name='RSIP' class="xxlarge" style="width: 210px; height: 60px;" disabled="disabled">{$pe.orderData.RSIP}</textarea>
可见的确未定义
4.更改为
<textarea name='RSIP' id='RSIP' class="xxlarge" style="width: 210px; height: 60px;" disabled="disabled">{$pe.orderData.RSIP}</textarea>
之后,代码正常
也可将js代码修改为
var rip = $("textarea[name='RSIP']").val();
去取得对应的元素数据
5.结论:发现难以判断的错误时,多使用 console.log
Uncaught TypeError: Cannot call method 'replace' of undefined 的解决
最新推荐文章于 2021-09-26 11:13:18 发布