首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Backbone-forms.js -如何从github "docs“中呈现示例中的提交按钮

Backbone-forms.js -如何从github "docs“中呈现示例中的提交按钮
EN

Stack Overflow用户
提问于 2013-01-30 02:04:01
回答 2查看 4K关注 0票数 4

这是backbone-forms中的代码。现在,我想呈现一个提交按钮来验证我的文本字段和内容,这是有文档记录的,除了如何将提交按钮放到dom中。请随意投反对票,不管怎样,在我看来,这对于新手来说是很难理解的

代码语言:javascript
复制
(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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-01 01:41:58

你需要一个模板,我的朋友!

虽然附加它是一种方式,但你应该坚持用BBF的方式来实现按钮。你可以创建不同的模板,或者在需要的时候重用它们,从这里你可以给你的表单添加属性等等。希望这能帮到你。

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

代码语言:javascript
复制
$(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!'); });

});

票数 5
EN

Stack Overflow用户

发布于 2013-01-30 05:00:59

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

代码语言:javascript
复制
$('yourBackboneform').append("<input type='submit' value='submit' name='submit' /><input type='reset' value='reset' name='reset' />");

backbone forms的主要目的是为移动设备提供ajax驱动的自动更新功能,所以我猜他们决定默认不呈现提交表单功能

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14589309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档