我有一个表单,其中使用声明性语法来设置窗口小部件。
<script>
require(["dojo/parser",
"dijit/form/Form",
"dijit/form/Button",
"dijit/form/ValidationTextBox",
"dijit/form/DateTextBox" ,
"dojox/validate/web",
"dojox/form/PasswordValidator",
"dijit/form/FilteringSelect" ,
"dijit/form/Button",
"dijit/form/Select",
"dojo/dom",
"dojo/dom-attr"
]);
<div data-dojo-type="dijit/form/Form" id="signupForm" data-dojo-id="signupForm"
encType="multipart/form-data" action="" method="">
<input type='hidden' name='csrfmiddlewaretoken' value='xxxxxxxxxxxx' />
<label for="id_username">Username:</label>
<input autofocus="autofocus" data-dojo-type="dijit/form/ValidationTextBox" id="id_username" maxlength="30" name="username" placeholder="Username" type="text" />
这个表单是从django模板构建的,我想使用django SelectDateWidget,它显示日期,月份和年份的三个小部件。问题是无法在模板中单独显示这些字段。我必须在浏览器中加载后添加dojo属性。像
这样的东西<script>
dojo.attr(dom.byId("id_birth_date_month"), {data-dojo-type:"dijit/form/Select", data-dojo-id:"monthSelect"});
dojo.attr(dom.byId("id_birth_date_year"), {data-dojo-type:"dijit/form/Select", data-dojo-id:"monthSelect"});
</script>
上述情况不起作用。我该怎么办?
答案 0 :(得分:1)
我找到了两种方法来做到这一点。我希望他们帮助别人。
首先是道场方式
<script>
require(["dojo/parser",
"dijit/form/Select",
"dojo/dom"], function (parser, Select, dom) {
dojo.attr(dom.byId("id_birth_date_month"), {"data-dojo-type":dijit/form/Select",
"data-dojo-id": "monthSelect"});
// similar lines for day and year
});
</script>
<!-- ...later in the code-->
<select id="id_birth_date_month">
...
</select>
<!-- two other similar selects for day and year-->
这是一个在浮动窗格中加载的表单,因此parser.parse()等已经运行。我不确定为什么声明语法在解析完成后仍然有效,但这种方法有效。
第二种方式来自django。在表单字段本身中定义属性。这是我采取的方法。
class SignupForm(Form):
# other form fields
birth_date = DateField(widget=SelectDateWidget(years=range(yearNow - 13, yearNow - 100, -1),attrs={"data-dojo-type": "dijit/form/Select", "data-dojo-id": "birthDateSelect"}), required=True, initial="", label=_('Birth Date'))
# other form fields
不幸的是,所有三个字段(年,月日)都获得相同的data-dojo-id,我不知道这是否是一个问题。暂时它起作用,我没有看到任何奇怪的东西。