作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误
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/
我是一名优秀的程序员,十分优秀!