Javascript Backbone-forms.js-如何呈现github示例中的提交按钮;“文件”;

Javascript Backbone-forms.js-如何呈现github示例中的提交按钮;“文件”;,javascript,html,backbone.js,backbone-forms,Javascript,Html,Backbone.js,Backbone Forms,这是主干表单中的代码。 现在,我想呈现一个提交按钮来验证我的文本字段和内容, 这是有文档记录的,除了如何将提交按钮放入dom。不管怎样,在我看来,这对noobs来说是很难理解的 (function($) { var cities = { 'UK': ['London', 'Manchester', 'Brighton', 'Bristol'], 'USA': ['Washington DC', 'Los Angeles', 'Austin', 'New Y

这是主干表单中的代码。 现在,我想呈现一个提交按钮来验证我的文本字段和内容, 这是有文档记录的,除了如何将提交按钮放入dom。不管怎样,在我看来,这对noobs来说是很难理解的

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

可以使用jQuery将submit按钮添加到表单中,如下所示

$('yourBackboneform').append("<input type='submit' value='submit' name='submit' /><input type='reset' value='reset' name='reset' />");
$('yourbackoneform')。追加(“”);

主干表单的主要用途是ajax驱动的移动自动更新功能,因此他们决定默认情况下不呈现提交表单功能,我想你需要使用BBF模板,我的朋友

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

检查工作演示:

$(函数(){
////设置模板----------------->
Backbone.Form.setTemplates({
customTemplate:“{fieldsets}}”
});
var城市={
“英国”:[“伦敦”、“曼彻斯特”、“布莱顿”、“布里斯托尔”],
“美国”:[“华盛顿特区”、“洛杉矶”、“奥斯汀”、“纽约”]
};
//形式
var form=新主干网({
////使用模板----------------->
模板:“customTemplate”,
模式:{
国家:{键入:'Select',选项:['UK','USA']},
城市:{键入:“选择”,选项:cities.UK}
}
}).render();
表单.on('country:change',函数(表单,countryEditor){
var country=countryEditor.getValue(),
新选项=城市[国家];
form.fields.city.editor.setOptions(newOptions);
});
//将其添加到页面中
$('body').append(form.el);
////用按钮做些什么----------------->
$(“#您的表单id”).submit(函数(){alert('BBF表单已提交!');返回false;});
$(“#您的表单取消”)。在('click',function(){alert('BBF表单被取消!');})上;
}))

$(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!'); });