我收到错误
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/