在 Opera 中使用 javascript 获取文本区域值

发布于 2024-10-09 06:45:03 字数 345 浏览 0 评论 0原文

我试图使用 Javascript 获取文本区域的值,检查它是否为空,但它在 Opera 中不起作用。在 IE、FF 和 Chrome 中它工作正常,但在 Opera 11 和 10 中它报告的值是空字符串,即使它有文本。 这是我的代码:

if (document.getElementById('mytextareaid').value.replace(/(^\s+|\s+$)/, '') == '') {
    alert('empty textarea');
}

使用 document.getElementById('mytextareaid').innerHTML 代替,也不起作用。我缺少什么?

I am trying to get the value of a textarea, to check if it's empty, using Javascript and it doesn't work in Opera. In IE, FF and Chrome it works fine, but in Opera 11 and 10 it reports the value to be the empty string, even if it has text.
Here's my code:

if (document.getElementById('mytextareaid').value.replace(/(^\s+|\s+$)/, '') == '') {
    alert('empty textarea');
}

Using document.getElementById('mytextareaid').innerHTML instead, doesn't work, either. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

握住我的手 2024-10-16 06:45:04

谢谢大家的帮助。事实证明,它适用于只有一个文本区域的简单页面,但在我的特定 HTML 文档中却没有。我终于在这里找到了解决方法:
JQuery val() 不适用于以下文本区域歌剧
我不知道到底是什么导致了这种奇怪的行为,但我确实知道那件事

Thank you all for your help. It turns out that it works with a simple page that only has a textarea, but in my particular HTML document it didn't. I finally found a workaround here:
JQuery val() does not work for textarea in Opera
I don't know what exactly caused the strange behavior, but I do know that the piece of

爱*していゐ 2024-10-16 06:45:04

引用我自己的话
JQuery val() 不适用于 Opera 中的文本区域

您可能遇到了 Opera 站点修补博客 ( http://my.opera.com/sitepatching/blog/facebook-and-some-core-patches ) 为“PATCH-287,Hack 让脚本看到输入的值在 blog.ebuddy.com 上的 TEXTAREA 中,Opera 无法从先前隐藏的文本区域中读取正确的值”。

在没有看到完整代码的情况下,我有点不愿意推荐解决方法。

然而,当我查看此内容时,我注意到将 textarea.contentEditable 设置为某些内容似乎可以让我随后读取该值......不过,这是一个奇怪的黑客行为,并且可能会给其他浏览器带来问题。

Quoting myself from
JQuery val() does not work for textarea in Opera :

You may have come across a very obscure bug referred to in a blog post on the Opera sitepatching blog ( http://my.opera.com/sitepatching/blog/facebook-and-some-core-patches ) as "PATCH-287, Hack to make script see typed value in TEXTAREA on blog.ebuddy.com. Opera fails to read correct value from a previously hidden textarea".

I'm a little bit reluctant to recomment workarounds without seeing the full code.

However, when I was looking at this I noticed that setting textarea.contentEditable to something seemed to let me read the value afterwards..it's a weird hack though, and it might cause problems for other browsers.

云醉月微眠 2024-10-16 06:45:03

替换成这个并尝试

if (document.getElementById('mytextareaid').innerHTML.replace(/(^\s+|\s+$)/, '') == '') {
    alert('empty textarea');
}

Replace with this and try

if (document.getElementById('mytextareaid').innerHTML.replace(/(^\s+|\s+$)/, '') == '') {
    alert('empty textarea');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文