【问题标题】:BootstrapValidator ignoring Enter keypress on complete formBootstrapValidator 忽略完整表单上的 Enter 按键
【发布时间】:2016-03-17 10:45:04
【问题描述】:

我们正在使用 bootstrapValidator 来验证我们所有的表单,但是在我们的登录表单上,如果用户按下 enterreturn 并且一切都有效,则什么都不会发生。

这是我们正在使用的 html:

<form id="signin" name="signin" class="validate-live" method="POST">
    <fieldset>
        <!-- Email -->
        <div class="form-group">
            <label class="control-label sr-only" for="email">Email</label>  
            <input id="email" name="email" type="email" placeholder="Email" 
                class="form-control"
                data-bv-notempty="true"
                data-bv-notempty-message="Please enter your email"
            >
        </div>

        <!-- Password -->
        <div class="form-group">
            <label class="control-label sr-only" for="password">Password</label>  
            <input id="password" name="password" type="password" placeholder="Password"
                class="form-control"
                data-bv-notempty="true"
                data-bv-notempty-message="Please enter your password"
            >
        </div>

        <button type="submit" id="submit_button" name="submit_button" class="btn btn-primary btn-block btn-next">Sign in</button>
        <a href="reset-password.php" class="btn btn-link-secondary btn-forgot" >Forgot password?</a>            
      </fieldset> 
    </form>

和 bootstrapValidator 代码:

$(document).ready(function() {
    $('.validate-live').bootstrapValidator({
        live: 'enabled',
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: '#submit_button',
        trigger: null
    }).on('error.field.bv', function(e, data) {
        data.bv.disableSubmitButtons(true);
    }).on('success.field.bv', function(e, data) {
        data.bv.disableSubmitButtons(false);
        if (data.bv.getInvalidFields().length>0) {
            data.bv.disableSubmitButtons(true);
        }
    });
});

我在事件处理程序中添加了一些 console.logs,它们显示验证器在按键时激活,如果表单无效,则会显示提示,但如果表单正常,则在用户实际点击之前什么都不会发生在#submit_button 按钮上。

【问题讨论】:

    标签: jquery validation bootstrapvalidator


    【解决方案1】:

    看不到任何分配submitButtons: '#submit_button'的理由

    只需删除 submitButtons: '#submit_button', 即可解决此问题。

    JS

    $(document).ready(function() {
      $('.validate-live').bootstrapValidator({
        live: 'enabled',
        message: 'This value is not valid',
        feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
        },
        //submitButtons: '#submit_button',
        trigger: null
      }).on('error.field.bv', function(e, data) {
        data.bv.disableSubmitButtons(true);
      }).on('success.field.bv', function(e, data) {
        data.bv.disableSubmitButtons(false);
        if (data.bv.getInvalidFields().length > 0) {
          data.bv.disableSubmitButtons(true);
        }
      });
    });
    

    HTML

    <form id="signin" name="signin" class="validate-live" method="POST">
      <fieldset>
        <!-- Email -->
        <div class="form-group">
          <label class="control-label sr-only" for="email">Email</label>
          <input id="email" name="email" type="email" placeholder="Email" class="form-control" data-bv-notempty="true" data-bv-notempty-message="Please enter your email">
        </div>
    
        <!-- Password -->
        <div class="form-group">
          <label class="control-label sr-only" for="password">Password</label>
          <input id="password" name="password" type="password" placeholder="Password" class="form-control" data-bv-notempty="true" data-bv-notempty-message="Please enter your password">
        </div>
    
        <button type="submit" id="submit_button" name="submit_button" class="btn btn-primary btn-block btn-next">Sign in</button>
        <a href="reset-password.php" class="btn btn-link-secondary btn-forgot">Forgot password?</a>
      </fieldset>
    </form>
    

    Working Fiddle

    【讨论】:

    • 如果我的表单有多个按钮怎么办? JS 代码保存在一个包含文件中,并在多个页面上使用,有很多不同的形式
    • 将所有其他按钮的 typesubmit 更改为 button
    • 看看这是否有帮助jsfiddle.net/tnzj9foy/1,还有其他方法可以将表单提交按钮与 bootstrapValidator 绑定,而不影响其他多个按钮或更改它们的类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2021-11-15
    • 2013-04-10
    • 2013-09-11
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多