2

これは私が学びたいドキュメントからの私のコードです。私の意見では、変更イベントが発生したときに、2番目の選択を非表示にして表示するのはクールです。いくつかの関数インジェクションを試しましたが、良い結果は得られませんでした。誰かがこの問題に2セントを与えることができますか?

(function($) {
    var cities = {
        'UK': ['London', 'Manchester', 'Brighton', 'Bristol'],
        'USA': ['Washington DC', 'Los Angeles', 'Austin', 'New York']
    };

    //The form
    var form1 = new Backbone.Form({
        schema: {
            country: { type: 'Select', options: ['UK', 'USA'] },
            city: { type: 'Select', options: cities.UK },
            message: { type: 'Text'}
        }
    }).render();

    form1.on('country:change', function(form1, countryEditor) {
        var country = countryEditor.getValue(),
            newOptions = cities[country];
            form1.fields.city.editor.setOptions(newOptions);

    });

    //Add it to the page
    $('body').append(form1.el);
})(jQuery);
4

1 に答える 1

2

form1.fields['メッセージ'].$el.hide();

レンダリング後と

form1.fields['メッセージ'].$el.toggle(); on change 関数内で問題を解決します。

(function($) {
    var cities = {
        'UK': ['London', 'Manchester', 'Brighton', 'Bristol'],
        'USA': ['Washington DC', 'Los Angeles', 'Austin', 'New York']
    };

    //The form
    var form1 = new Backbone.Form({
        schema: {
            country: { type: 'Select', options: ['UK', 'USA'] },
            city: { type: 'Select', options: cities.UK },
            message: {  
                //fieldAttrs: { style: 'width: 400px; height:300px;' }, 
                type: 'TextArea',
                fieldClass: 'message-input', 
                validators: [ 'required', /.{20,}/ ] 
            }
        }
    }).render();
    **form1.fields['message'].$el.hide();**

    *form1.on('country:change',* function(form1, countryEditor) {
        var country = countryEditor.getValue(),
            newOptions = cities[country];
            form1.fields.city.editor.setOptions(newOptions);
            **form1.fields['message'].$el.toggle();**
    });

    //Add it to the page
    $('body').append(form1.el);
})(jQuery);
于 2013-01-29T20:25:37.987 に答える