在jQuery中设置textarea的值

左心房为你撑大大i 提交于 2020-02-27 00:36:46

我正在尝试使用jquery通过以下代码在textarea字段中设置值:

$("textarea#ExampleMessage").attr("value", result.exampleMessage);

问题是,一旦执行此代码,就不会更改textarea中的文本吗?

但是,当执行alert($("textarea#ExampleMessage").attr("value")) ,是否返回新设置的值?


#1楼

当页面中有JQuery v1.4.4时,这些都不起作用。 当将JQuery v1.7.1注入我的页面时,它终于可以工作了。 因此,就我而言,是导致问题的原因是我的JQuery版本。

id ==> textareaid

======================

var script1 = document.createElement("script");
script1.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
document.body.appendChild(script1);

var script2 = document.createElement("script"); 
script2.type = "text/javascript"; 
script2.innerHTML = "var $jq171 = $.noConflict();"; 
document.body.appendChild(script2);

$jq171('#textareaid').val('xxx');

#2楼

噢,男孩们! 它与

$('#your_textarea_id').val('some_value');

#3楼

文本区域没有价值。 jQuery .html()在这种情况下有效

$("textarea#ExampleMessage").html(result.exampleMessage);

#4楼

我遇到了同样的问题,该解决方案无效,但有效的方法是使用html

$('#your_textarea_id').html('some_value');

#5楼

我试着用.val() .text() .html()并有一些错误使用jQuery读取或文本区域的设定值...我endup使用本地的js

$('#message').blur(function() {    
    if (this.value == '') { this.value = msg_greeting; }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!