4

这是来自主干形式的代码。现在,我想呈现一个提交按钮来验证我的文本字段和记录的内容,除了如何将该提交按钮放入 dom 中。随意投反对票,无论如何,在我看来,这对于新手来说很难弄清楚

(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

2 回答 2

5

我的朋友,您需要使用 BBF 模板!

虽然附加它是一种方法,但您应该真正坚持 BBF 实现按钮的方式。您可以创建不同的模板或在必要时重新使用它们,并从这里您可以将属性添加到您的表单等。。希望这可以帮助...

检查工作演示:http: //jsfiddle.net/c5QHr/116/

$(function() {

//// Set the template -----------------------------> 
Backbone.Form.setTemplates({
    customTemplate: '<form id="your-form-id">{{fieldsets}}<input id="your-form-cancel" type="reset" value="cancel" name="reset" /><input type="submit" value="submit" name="submit" />'

});

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

//The form
var form = new Backbone.Form({
    //// Use the template ----------------------------->
    template: 'customTemplate',
    schema: {
        country: { type: 'Select', options: ['UK', 'USA'] },
        city: { type: 'Select', options: cities.UK }
    }
}).render();

form.on('country:change', function(form, countryEditor) {
    var country = countryEditor.getValue(),
        newOptions = cities[country];

    form.fields.city.editor.setOptions(newOptions);
});

//Add it to the page
$('body').append(form.el);

////Do something with the buttons ----------------------------->

$("#your-form-id").submit(function(){alert('BBF Form was submitted!'); return false;});
$("#your-form-cancel").on('click',function(){alert('BBF Form was cancelled!'); });

});

于 2013-03-31T17:41:58.290 回答
1

可以像这样使用 jQuery 在表单中添加提交按钮

$('yourBackboneform').append("<input type='submit' value='submit' name='submit' /><input type='reset' value='reset' name='reset' />");

主干表单的主要目的是 ajax 驱动的自动更新移动设备的疯狂东西,所以他们决定默认不呈现提交表单的东西,我猜

于 2013-01-29T21:00:59.053 回答