jQuery .attr('value') 为文本区域返回未定义 [英] jQuery .attr('value') returns undefined for text area

查看:33
本文介绍了jQuery .attr('value') 为文本区域返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,它通过 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 {} 子句末尾的部分触发 TextAreainput type="text" 元素,但由于某种原因它总是看到 cfe.attr('value');TextAreaundefined.为此,我使用 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').