gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'match' of undefined when iterating through index

转载 作者:行者123 更新时间:2023-11-27 22:35:18 27 4
gpt4 key购买 nike

我收到错误

Uncaught TypeError: Cannot read property 'match' of undefined

当我尝试运行此代码时:

var str = 'exercise_files_attributes_' + index + '_content';
var textarea = $('textarea[id=str]');
alert(textarea.val());

基本上我尝试获取文本区域的内容,但在警报中我只收到消息“未定义”。

当我通过

获取文本区域时
var textarea = $('textarea[id="exercise_files_attributes_1_content"]')]');

一切正常。

当我打印索引值时,我也得到正确的响应。

这可能是什么问题?

最佳答案

您的选择器中有错误,请参阅此处。

var str = 'exercise_files_attributes_' + index + '_content';
var textarea = $('textarea[id=str]'); // <-- str is a string and -----
alert(textarea.val()); -
-
$('textarea[id="' + str + '"]'); // < -- should be like this ---------

您使用了 str 作为字符串,这就是 JavaScript 找不到您的元素的原因。

关于javascript - 未捕获的类型错误 : Cannot read property 'match' of undefined when iterating through index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com