具有变形的动态形式

Eve*_*Eve 1 python pylons deform colander

使用变形制作表单,并希望根据用户的选择更改pageShema类.防爆.如果他从selectwidget中选择选项1,则向他显示一组字段,如果是其他选择的情况 - 另一个.这该怎么做?

Mat*_*boz 5

您可以使用jquery并使用"name"属性进行选择.此外,您可以使用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())
Run Code Online (Sandbox Code Playgroud)

然后,在您的模板中添加以下内容:

<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>
Run Code Online (Sandbox Code Playgroud)