the*_*unt 4 html javascript backbone.js backbone-forms
这是骨干表格的代码.现在,我想呈现一个提交按钮来验证我的文本字段和内容,这是记录的,除了如何将提交按钮放入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);
Run Code Online (Sandbox Code Playgroud)
你需要使用我的朋友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!'); });
Run Code Online (Sandbox Code Playgroud)
});