gpt4 book ai didi

javascript - 如何在 `HTML label`之前动态添加 `ValidationTextBox`?

转载 作者:行者123 更新时间:2023-12-03 09:41:48 27 4
gpt4 key购买 nike

我需要在每个 ValidationTextBox 之前动态添加 HTML 标签

ValidationTextBox 和 HTML 标签是根据对象 data 的属性数量创建的。

我需要一个关于如何做到这一点的示例。

示例: http://jsfiddle.net/F2qAN/97/#run

dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.layout.ContentPane");

function build() {
var data = {
name: 'a',
surname: 'b',
age: 'c'
},
validationTextBox,
layout = new dijit.layout.ContentPane({});
Object.keys(data).forEach(function (prop, index) {
validationTextBox = new dijit.form.ValidationTextBox({
id: prop + '-'+ index,
name: prop,
value: data[prop]
});
layout.addChild(validationTextBox);
}.bind(this))
layout.placeAt('layout');
layout.startup();
}

dojo.addOnLoad(build);

我正在尝试使用dojo/dom-construct但没有结果,例如:

        var label = domConstruct.toDom("<label for="male">label</label>");
layout.addChild(validationTextBox);
layout.addChild(label );

最佳答案

使用自制的小部件是正确的方法。

require(['dijit/form/ValidationTextBox', 'dijit/layout/ContentPane', 'dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dojo/_base/declare', 'dojo/domReady!'], function(ValidationTextBox, ContentPane, _WidgetBase, _TemplatedMixin, declare, domReady) {
var data = {
name: 'a',
surname: 'b',
age: 'c'
},
layout = new ContentPane(),
LabelTextbox = declare([_WidgetBase, _TemplatedMixin], {
label: '',
textboxId: '',
name: '',
value: '',

templateString: '<div><label>${label}</label><div data-dojo-attach-point="textboxNode"></div></div>',

postCreate: function() {
this.inherited(arguments);

this.own(new ValidationTextBox({
id: this.textboxId,
name: this.name,
value: this.value
}, this.textboxNode));
}
});

Object.keys(data).forEach(function (prop, index) {
layout.addChild(new LabelTextbox({
textboxId: prop + '-'+ index,
name: prop,
value: data[prop],
label: 'foo'
}));
}.bind(this));


layout.placeAt('layout');
layout.startup();

});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" media="screen">
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<div id="layout"></div>

关于javascript - 如何在 `HTML label`之前动态添加 `ValidationTextBox`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31162782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com