JQuery val() 不适用于 Opera 中的文本区域
我正在使用 jQuery 显示模式对话框。该对话框上有一个 textarea
控件。但是,在提交此对话框时,由于某种原因,jQuery 无法识别此 textarea
的值:它始终为空。 这在其他浏览器中完美运行。我设置了警报来显示该值,但它看起来是空白的。有人可以在这方面帮助我吗?
控件:
<input type="text" id="txtGroupName"/>
<textarea rows="3" cols="30" id="txtDescription"></textarea>
使用此值的 jQuery 代码:
var postData = new Object();
postData.GroupName = $('#txtGroupName').val();
postData.Description = $('#txtDescription').val();
$('#txtDescription').val()
为空,但 $('#txtGroupName').val()
被读取正确,因为它是一个输入字段。
关于此问题的另一个发现:
当我在页面加载时填充控制值后将警报放入更新函数中时,此警报会正确显示现有值。但它仅显示现有价值。提交模态框后不显示编辑的值。
I am displaying a modal dialog using jQuery. This dialog has a textarea
control on it. But while submitting this dialog, the value of this textarea
is not recognized by jQuery for some reason: it always comes blank.
This works perfectly in other browsers. I put alert to display the value but it looks blank. Can anybody help me in this regards?
Controls:
<input type="text" id="txtGroupName"/>
<textarea rows="3" cols="30" id="txtDescription"></textarea>
jQuery code which used this value:
var postData = new Object();
postData.GroupName = $('#txtGroupName').val();
postData.Description = $('#txtDescription').val();
$('#txtDescription').val()
comes blank but $('#txtGroupName').val()
is read correctly as it is a input field.
One more finding about this issue:
When I put alert in my update function after populating the control value on page load, this alert displays the existing value properly. But it displays only existing value. It does not display the edited value after submitting the modal box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
jquery 中的 val() 和 text() 工作正常,但是在设置 textarea 的值后,您需要
重新渲染文本区域,您可以通过这种方式设置 css 属性,
您好,来自俄罗斯。抱歉我的英语=)
val() and text() in jquery works correctly, but after setting value of textarea you need to
rerender textarea, you can do this setting css property in such way
Hello from Russia. Sorry for my english =)
您是否尝试过
.attr("text")
或.attr("value")
?我无法对此进行测试,但这对我来说似乎是合乎逻辑的。如果没有,请告诉我,我将删除此答案。
Have you tried
.attr("text")
or.attr("value")
? I'm unable to test this but this would seem logical to me.If it doesn't then let me know and I'll remove this answer.
您可能遇到了 Opera 站点修补博客 1 as "PATCH-287,Hack 使脚本可以看到 blog.ebuddy.com 上 TEXTAREA 中键入的值。Opera 无法从之前隐藏的值中读取正确的值。文本区域”。
不过,在没有看到完整代码的情况下,我有点不愿意推荐解决方法。
you may have come across a very obscure bug referred to in a blog post on the Opera sitepatching blog 1 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 though.
大家好,
我在 Opera 10.63 和 Windows 上也有同样的问题。
Javier Canizalez 建议的 hack 有效,但前提是我不再重复使用对话框(和文本区域)。然而,事实并非如此。通过他的黑客,加载页面并单击某个项目后,我会显示一个先前隐藏的对话框(显示:无),其中包含文本区域。第一次一切都工作正常(通过黑客)。关闭对话框后 /* $(dialog).hide()); */ 并通过单击另一个项目再次重用它,黑客不再起作用,并且 javascript/jQuery 不再获取新键入的值,直到整个页面重新加载。
我在上面给出的链接之一发现 Opera 的人已经解决了这个问题:PATCH-287
但这对我来说似乎并不固定:)我在那里写了一个问题,看看他们是否会回复:
opera patch-287
有人设法找到解决方法吗这?
谢谢您并致以最诚挚的问候。
Good day people,
I too have the same problem with Opera 10.63 and Windows.
The hack suggested by Javier Canizalez works, but only as long as I don't reuse the dialog (and the textarea) again. However, this is not the case. With his hack, after the page is loaded and I click on an item, I display a dialog that was previously hidden (display:none) with textarea inside it. Everything works fine the first time (with the hack). After closing the dialog /* $(dialog).hide()); */ and reusing it again by clicking on another item, the hack does not work anymore and javascript/jQuery does not get the new typed value anymore until a full page reload.
I found at one of the links given above that the guys at opera have fixed that problem: PATCH-287
But it does not seems fixed to me :) I wrote a question there and will see if they'll reply:
opera patch-287
Has someone managed to get a workaround this?
Thank you and best regards.
我发现,在 Ubuntu 10.04 上的 Chrome 6.0.472.59、Firefox 3.6.9 和 Opera 10.62 中,
textarea
确实具有/使用.val()
属性。万一其他浏览器不支持或可能不支持,我整理了这个 jsbin 演示。不过,我使用了 if/else 块来涵盖这两种方法。以防万一...这个 Stackoverflow 问题 (jQuery get textarea text) 也建议它应该是可能且可靠的,就像 Val() 的 API 页面上的第一个评论者一样,位于 < a href="http://www.jquery.com/" rel="nofollow noreferrer">jQuery.com。
请注意,关于 Opera:jsBin 演示仅在我停用开发人员工具(无论出于何种原因)后才起作用。可能值得关闭 Dragonfly(如果它正在运行),然后刷新演示页面(或者显然是您自己的测试页面)以查看是否有所不同。无论哪种方式,总是值得清除缓存以确保使用最新版本的文件。
I've found, in Chrome 6.0.472.59, Firefox 3.6.9 and Opera 10.62, all on Ubuntu 10.04, that
textarea
does have/use the.val()
attribute. On the off-chance that some other browsers don't, or might not, I put together this jsbin demo. I used anif/else
block to cover both approaches, though. Just in case...This Stackoverflow question (jQuery get textarea text) also suggests that it should be possible and reliable, as does the first commenter on the API page for Val(), at jQuery.com.
Note, as regards Opera: the jsBin demo only worked once I'd deactivated the developer tools (for whatever reason). It might be worth turning off Dragonfly (if it's running), and then refreshing the demo page (or, obviously, your own test page) to see if it makes a difference. Either way, it's always worth clearing your cache to make sure the most up-to-date version of the files are being used.
我使用这个解决方法:
I use this workaround:
在 Opera 中,仅用于获取值或文本区域:
document.getElementById("description").value;
奇怪的是 $("textara#description").val("") 有效(设置方法)
In opera for getting the value or a textarea works only:
document.getElementById("description").value;
strange is that $("textara#description").val("") works (set method)
通过属性名称而不是 id 选择
Select
<textarea>
by attribute name instead of id.Textarea 没有 value 属性。尝试使用
Textarea doesn't have a value attribute. Try to use
文本区域中使用它来修复此问题
我在脚本末尾的 。
我对我的英语感到抱歉
I fix this using this in textarea
put at end of script.
I am sorry for my english