【问题标题】:How to check input field in bootstrap modal如何检查引导模式中的输入字段
【发布时间】:2013-08-13 09:19:14
【问题描述】:

我的主干视图如下:

app.View.WelcomeProfil = Backbone.View.extend({
initialize : function(){
this.header = document.createElement('div');
this.$header = $(this.header);
this.render();
},

model : new app.Model.AccountProfile(),

template : {
header : _.template($('#welcome-profile-header').html()),
body : _.template($('#welcome-profile').html()),
error : _.template($('#welcome-profile-error').html())
},

attributes : {
'class' : 'form-horizontal row-fluid'
},

tagName : 'form',

next : function(){
var self = this;

_.each(this.$el.serializeArray(), function(value){
  self.model.set(value.name, value.value);
});

this.model.save(null, {
  success : function(model){
    self.trigger('next');
  },
  error : function(model){
    self.$el.append(self.template.error());
  }
});
},

previous : function(){
 this.trigger('previous');
},

render: function(){
var self = this;

this.$header.html(this.template.header());
this.model.fetch({
  success : function(model){
    self.$el.html(self.template.body(model.toJSON()));
  },
  error : function(model){
    self.$el.html(self.template.body(model.toJSON()));
    self.$el.append(self.template.error());
  }
});
return this;
},

destroy : function(){
  this.$header.remove();
  this.remove();
  this.trigger('remove', this);
}
});

我的模板如下所示

<script type="text/template" id="welcome">
<div class="modal-header">
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
  <@ if (previous) { @><input type="button" class="pull-left" value="<fmt:message key="foobar.form.previous"/>"/><@ } @>
  <@ if (next) { @><input type="button" class="pull-right" value="<fmt:message key="foobar.form.next"/>"/><@ } @>
  <@ if (finish) { @><input type="button" class="pull-right" value="<fmt:message key="foobar.form.finish"/>"/><@ } @>
</div>

这是插入到modal-body里面的模板

<script type="text/template" id="welcome-profile">
<fieldset>
<div class="control-group">
  <label class="control-label" for="firstName"><fmt:message key="foobar.user.firstName"/></label>
  <div class="controls">
    <input type="text" class="input-block-level" name="firstName" required="required" placeholder="<fmt:message key="foobar.user.firstName"/>" value="<@= firstName @>">
  </div>
</div>
<div class="control-group">
  <label class="control-label" for="lastName"><fmt:message key="foobar.user.lastName"/></label>
  <div class="controls">
    <input type="text" class="input-block-level" name="lastName" required="required" placeholder="<fmt:message key="foobar.user.lastName"/>" value="<@= lastName @>">
  </div>
</div>
<div class="control-group">
  <label class="control-label" for="jobTitle"><fmt:message key="foobar.user.jobTitle"/></label>
  <div class="controls">
    <input type="text" class="input-block-level" name="jobTitle" required="required" placeholder="<fmt:message key="foobar.user.jobTitle"/>" value="<@= jobTitle @>">
  </div>
</div>

我希望当用户单击下一步时...我应该能够检查名字、姓氏和职位字段是否已填写..?这可能吗?

【问题讨论】:

    标签: backbone.js backbone-views backbone-routing


    【解决方案1】:

    在您的模型上创建一个validate 方法,如下所示:http://backbonejs.org/#Model-validate

    创建后,主干会在运行保存之前自动调用此方法,您可以绑定到invalid 事件以更新错误的视图。

    【讨论】:

    • 我如何将 validate 方法放入我的视图中,因为我认为单击下一个按钮时我需要做一些事情
    • 点击下一个按钮时,调用model.isValid,如果为false,则检查validationError,这两个都是我提供的链接后的第一个和第二个属性/方法...
    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 2015-11-13
    • 2018-08-09
    • 2016-11-08
    • 2016-02-01
    • 2023-03-31
    • 2020-05-23
    相关资源
    最近更新 更多