javascript Dijit.Form.Textarea 的设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5107722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:57:49  来源:igfitidea点击:

Set Value of Dijit.Form.Textarea

javascriptdojodijit.form

提问by Isaac Levin

I have a dijit dialog that contains a form that I want to auto-populate. I can get the dialog to display with the form in it, but I have been unable to set the value of a text area within the form. Here is the div that houses the html.

我有一个 dijit 对话框,其中包含一个我想要自动填充的表单。我可以让对话框与其中的表单一起显示,但是我一直无法在表单中设置文本区域的值。这是存放 html 的 div。

<div dojoType="dijit.Dialog" id="formDialog" title="Form Dialog" >
<table>
    <tr>
        <td>
            <label for="desc">
                Description:
            </label>
        </td>
        <td>

        <textarea id="desc" name="desc" dojoType="dijit.form.Textarea" style="width:200px;"></textarea>

SAVE CLOSE

保存关闭

I can get this to display just fine by doing

我可以通过这样做来让它显示得很好

var formDlg = dijit.byId("formDialog"); formDlg.show();

var formDlg = dijit.byId("formDialog"); formDlg.show();

But the issue I have is setting the value of the textarea called "desc". I have tried multiple things, but I know I need to

但我遇到的问题是设置名为“desc”的文本区域的值。我尝试了多种方法,但我知道我需要

var test = dijit.byId("desc");

but if I set any property of test, such as

但是如果我设置了测试的任何属性,例如

   test.value = "foo";
   test.textContent = "foo";
   test.innerHTML = "foo";
   test.srcNodeRef = "foo";

The value is never saved and displayed inside the textarea. Is there a trick to doing this? Any help would be great. Thanks

该值永远不会保存并显示在 textarea 中。这样做有技巧吗?任何帮助都会很棒。谢谢

回答by Frode

var test = dijit.byId("desc");
test.set("value", "foo");

..should do the trick, I think. Most widgets in Dojo use the setmethod (formerly attr) to set property values, instead of manipulating them directly like you've tried to do. You can also set multiple properties in one go by passing an object:

..应该能解决问题,我想。Dojo 中的大多数小部件都使用set方法(以前称为attr)来设置属性值,而不是像您尝试的那样直接操作它们。您还可以通过传递一个对象一次性设置多个属性:

var test = dijit.byId("desc");
test.set({"value": "foo", "name": "someName"});

回答by Waqas_Ashraf

For some reason, dijit.byId("txtAreaMytextarea").set("value", "somevalue")does not work with TextAreabut works with other dijit types when you use Dojo 1.6 and use dijit.form.SimpleTextareaas TextArea. The function setValue("")also doesn't work.

出于某种原因,dijit.byId("txtAreaMytextarea").set("value", "somevalue")不能一起工作TextArea,但是当你使用其他的dijit类型的作品道场1.6和使用dijit.form.SimpleTextareaTextArea。该功能setValue("")也不起作用。

If this happens to you, try using dojo.byIdinstead of dijit.byIdand just setting value by doing

如果您遇到这种情况,请尝试使用dojo.byId而不是dijit.byId通过执行来设置值

dojo.byId("txtAreaMytextarea").value = "somevalue";