Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 防止在主干上验证后重新加载页面_Javascript_Html_Backbone.js - Fatal编程技术网

Javascript 防止在主干上验证后重新加载页面

Javascript 防止在主干上验证后重新加载页面,javascript,html,backbone.js,Javascript,Html,Backbone.js,我有这样的看法: return Backbone.Marionette.ItemView.extend({ template: template, ui: { form: '#login', button: '#submitbutton' }, onRender: function() { this.ui.form.on('submit', function(e) { // e.preventDefau

我有这样的看法:

 return Backbone.Marionette.ItemView.extend({ 
    template: template,

    ui: {
      form: '#login',
      button: '#submitbutton'
    },

    onRender: function() {
      this.ui.form.on('submit', function(e) {

        // e.preventDefault();

        mylogin = new login();

        mylogin.save({boxid:$("#boxid").val(),password:$("#passwordid").val(),validate:true});

             vent.trigger('navigate', 'home');

        // if(myLogin.validationError) {
        //     vent.trigger('navigate', 'home');
        // }

        return false;
      });
    }
  });
这个模型:

return Backbone.Model.extend({



  validate: function(attrs, options){
    if(attrs.boxid.length < 10)
      {
        return "user id must be more than 10 characters";
        // return "BoxID should be greater than 10";
      }
   else if(attrs.password.length < 10)
      { 
         return "password must be more than 10 characters";
        // return "Password should be greater than 10";
      }
   else if((attrs.boxid!=myBoxid)||(attrs.password!=myPassword))
      {
          return "Your login credentials are incorrect";  
      }

  },





   });
return Backbone.Model.extend({
验证:功能(属性、选项){
如果(attrs.boxid.length<10)
{
返回“用户id必须超过10个字符”;
//返回“BoxID应大于10”;
}
否则如果(attrs.password.length<10)
{ 
return“密码必须超过10个字符”;
//返回“密码应大于10”;
}
else if((attrs.boxid!=myBoxid)| |(attrs.password!=myPassword))
{
返回“您的登录凭据不正确”;
}
},
});
此登录html模板:

<div class>
    <div>Information</div>
    <div>
        <form id="login" name="login" method="post">  
            <div>
                <label for="boxid">BoxID</label>  
                <input id="boxid" name="box" placeholder="Enter your box ID">
            </div>
            <div>
                <label for="password">Password</label>  
                <input id="passwordid" type="password" name="password" placeholder="Password">
            </div>
            <label id="errormsg"></label>
            <button id="submitbutton">Login</button>
        </form>  
    </div>
</div>

问询处
BoxID
密码
登录

问题是,每次用户输入错误的凭据时,页面都会重新加载,有什么方法可以防止这种情况发生吗?

要做到这一点,您只需返回
e.preventDefault()

更重要的是,该模型没有
url
函数


您是否尝试删除此行的注释:
//e.preventDefault()
?@akoskm如果我这样做,页面不会重新加载,但存在
未捕获错误:必须指定与模型相关的“url”属性或函数,请参阅我的答案
return Backbone.Model.extend({
    url: function () {
        // see the docs, it can be a url string or a function
    }
    validate: function(attrs, options){
    },
});