1

変形でフォームを作成し、ユーザーの選択に応じて pageShema クラスを変更したいと考えています。元。彼が selectwidget からオプション 1 を選択した場合は、フィールドの 1 つのセットを表示し、他の選択の場合は別のフィールドを表示します。これを行う方法?

4

1 に答える 1

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())

次に、テンプレートに次のようなものを追加します。

<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>
于 2016-04-29T14:15:54.230 に答える