使用deform制作表单,并希望根据用户的选择更改pageShema类。例如。如果他从selectwidget中选择了选项1,则向他显示一组字段,如果是其他选择-另一种情况。该怎么做呢?
发布于 2016-04-29 22:15:54
您可以使用jquery和带有"name“属性的select。此外,您还可以使用jquery "parent()“函数来获取您感兴趣的显示/隐藏输入的容器。
例如,在您的模式中执行如下操作:
# This is the "<select>"
choices = (('yes','Yes'),
('no', 'No'))
bar = colander.SchemaNode(colander.String(),
widget=deform.widget.SelectWidget(values=choices))
# This is the input that should appear only when the "yes" value is selected
foo = colander.SchemaNode(colander.String())然后,在您的模板中添加如下内容:
<script>
$( document ).ready(function() {
// ensure that the "foo" input starts hidden
var target = $("input[name=foo]").parent().parent();
target.hide();
$("select[name=bar]").on('change', function(){
var valueSelected = this.value;
if (valueSelected == "yes") {
target.show();
} else {
target.hide();
}
});
});
</script>https://stackoverflow.com/questions/34223948
复制相似问题