jQuery .attr('value') 返回文本区域未定义
我有一个页面,它通过 ajax 动态引入表单并将其显示在模态 div 中(位于覆盖整个页面的覆盖 div 之上)。这是为了让他们在窗口关闭之前保存某些数据。除了一件事之外,一切都很好。
$('#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
的未定义
。为此,我使用 FF6.0 和 jQuery 1.6。
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');
}
}
});
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用
.val()
而不是.attr('value')
。Try
.val()
instead of.attr('value')
.<textarea>
doesn't have avalue
attribute (the text is between the tags, not invalue=""
) however I believe.val()
will retrieve it.对于文本区域,请使用 :
代替。
jQuery 获取文本区域文本
在 jQuery 中设置 textarea 的值
For textareas use :
instead.
jQuery get textarea text
Set value of textarea in jQuery
TextArea 没有名为 value 的属性...尝试使用 val
TextArea does not have an attribute called value...try using val
默认情况下,
textarea
没有value
属性。您应该使用cfe.val()
或cfe.html()
来获取其内容。textarea
does not have avalue
attribute by default. You should usecfe.val()
orcfe.html()
to get its content.这是因为 textarea 没有 value 属性。
请改用
.val()
。It's because a textarea doesn't have a value attribute.
Use
.val()
instead.好的,原因是 jQuery 1.6 区分了创建时的属性和当前的属性值。您创建的文本区域很可能没有值属性;因此,初始值为
未定义
。当您想要提取定义的值时,您需要使用.prop('value')
,或调用更有用的.val()
方法。Ok, so the reason is that jQuery 1.6 makes a distinction between the attribute at creation and your current property value. You've created the textarea without a value attribute more than likely; thus, the initial value is
undefined
. When you want to pull the defined value, you need to either use.prop('value')
, or call the more helpful.val()
method.