jQuery .attr('value') 返回文本区域未定义

发布于 2024-12-01 03:35:17 字数 1132 浏览 5 评论 0原文

我有一个页面,它通过 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 {} 子句中的部分触发 TextAreainput 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我闹别瞎闹 2024-12-08 03:35:17

尝试使用 .val() 而不是 .attr('value')

Try .val() instead of .attr('value').

<textarea> doesn't have a value attribute (the text is between the tags, not in value="") however I believe .val() will retrieve it.

浅沫记忆 2024-12-08 03:35:17

对于文本区域,请使用 :

 $("#textareaid").val() or $("#textareaid").html()

代替。

jQuery 获取文本区域文本

在 jQuery 中设置 textarea 的值

For textareas use :

 $("#textareaid").val() or $("#textareaid").html()

instead.

jQuery get textarea text

Set value of textarea in jQuery

转角预定愛 2024-12-08 03:35:17

TextArea 没有名为 value 的属性...尝试使用 val

map[cfe.attr('name')] = cfe.val();

TextArea does not have an attribute called value...try using val

map[cfe.attr('name')] = cfe.val();
日暮斜阳 2024-12-08 03:35:17

默认情况下,textarea 没有 value 属性。您应该使用 cfe.val()cfe.html() 来获取其内容。

textarea does not have a value attribute by default. You should use cfe.val() or cfe.html() to get its content.

茶色山野 2024-12-08 03:35:17

这是因为 textarea 没有 value 属性。

请改用.val()

It's because a textarea doesn't have a value attribute.

Use .val() instead.

や三分注定 2024-12-08 03:35:17

好的,原因是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文