jQuery .attr('value') 为文本区域返回未定义 [英] jQuery .attr('value') returns undefined for text area
问题描述
我有一个页面,它通过 ajax 动态引入表单并将其显示在模式 div 中(一个位于覆盖整个页面的覆盖 div 上方).这是为了让他们在窗口关闭之前保存某些数据.除了一件事之外,一切都很好.
I have a page which dynamically brings in a form via ajax and displays it in a modal div (one that sits above an overlay div that covers the entire page). This is to let them save certain data before a window closes. Everything works great except one thing.
$('#save_close_form').find('*[name]').each(function(index, form_element) {
var cfe = (form_element.jquery == undefined ? $(form_element) : form_element);
console.log(cfe.attr('name') + " => " + cfe.attr('value'));
if (cfe.attr('name').match(/data[/)) {
if (cfe.attr('type') == 'checkbox') {
if (cfe.attr('checked')) {
map[cfe.attr('name')] = 'on';
}
else {
map[cfe.attr('name')] = '';
}
}
else if (cfe.attr('type') == 'radio') {
// only get checked radio buttons
if (cfe.attr('checked')) {
map[cfe.attr('name')] = cfe.attr('value');
}
}
else {
map[cfe.attr('name')] = cfe.attr('value');
}
}
});
else {} 子句末尾的部分触发 TextArea
和 input type="text"
元素,但由于某种原因它总是看到 cfe.attr('value');
为 TextArea
的 undefined
.为此,我使用 FF6.0 和 jQuery 1.6.
The part in the else {} clause at the end triggers for TextArea
and input type="text"
elements, but for some reason it always sees cfe.attr('value');
as undefined
for the TextArea
. I'm using FF6.0 with jQuery 1.6 for this.
推荐答案
尝试 .val()
而不是 .attr('value')
.
没有
value
属性(文本在标签之间,不在 value=""
中)但是我相信 .val()
会检索它.
<textarea>
doesn't have a value
attribute (the text is between the tags, not in value=""
) however I believe .val()
will retrieve it.
这篇关于jQuery .attr('value') 为文本区域返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!