【问题标题】:Disable Parsley validation from disabled fields dynamically动态禁用禁用字段的 Parsley 验证
【发布时间】:2018-12-30 11:54:39
【问题描述】:

我正在使用 Parsley 验证我的表单。

我重新编写了很多 StackOverflow 问题。但没有什么对我有用

问题是一开始我有两个禁用的密码字段,它们将通过一个复选框启用:

$('#chk_change_pass').on('change', function() {
    if ($(this).is(':checked')) {
      $('[name=password]')
      .prop('disabled', false);
    }
    else {
      $('[name=password]')
      .prop('disabled', true);
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form data-parsley-validate>
    <input type="checkbox" id="chk_change_pass">
    <input type="password" name="password" data-parsley-excluded="[disabled]" disabled required>
    <input type="submit">
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.8.1/parsley.min.js"></script>

禁用后不排除在启动时也不排除

【问题讨论】:

    标签: javascript jquery validation parsley.js


    【解决方案1】:

    您需要使用change,而不是ifChanged 来触发复选框更改

    $('#chk_change_pass').on('change', function() {
        if ($(this).is(':checked')) {
          $('[name=password]')
          .prop('disabled', false);
        }
        else {
          $('[name=password]')
          .prop('disabled', true);
        }
      });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <form data-parsley-validate>
        <input type="checkbox" id="chk_change_pass">
        <input type="password" name="password" data-parsley-excluded="[disabled]" disabled >
        <input type="submit">
    </form>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.8.1/parsley.min.js"></script>

    【讨论】:

    • 感谢您的编辑,您是对的。我应该更改活动名称。但这不是问题。我忘记向 sn-p 添加“必需”验证。不是你可以看到输入被验证,即使它被禁用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2017-03-24
    • 2015-02-07
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多