【问题标题】:why isn't the jquery validation running?为什么没有运行 jquery 验证?
【发布时间】:2016-12-21 06:18:28
【问题描述】:

我有这个包含所有其他输入的表单,所有输入都正确显示错误,除了这个是文件上传。

谁能给我一双眼睛,看看我哪里做错了?

当我 console.log(element) 时,应该会显示错误发生的时间,但它甚至根本没有发生。

这是我的html代码

<label for="phone">Phone</label><input type="text" name="phone" value="">
<label for="city">City</label><input type="text" name="city" value="">
<label for="qr_code_image">QR CODE</label><input type="file"  class="hidden image-field" name="qr_code_image" id="qr_code_image"><div class="add-image-icon" data-field="qr_code_image"><i class="icon-plus"></i></div>

在我的 js 中

           rules: {
                city: {
                    required: true
                },
                phone: {
                    required: true,
                },
                qr_code_image: {
                    required: true
                }
            },
            // Where to place the error message
            errorPlacement: function (error, element) {
                console.log(element);
                error.appendTo(element.next('.errorMsg'));
            }

我没有在此处添加错误输出的 div。 使用控制台日志,我只能看到两个错误而不是 3 个。 我尝试提交,甚至没有点击图片上传。

编辑:我意识到这两个类都在使用display: none !important

有没有办法保留 css 并仍然使验证工作?

【问题讨论】:

  • 我不确定,但也许 .hidden 被添加为默认字段以被忽略。您可以使用以下内容覆盖“忽略”字段:'$("#myform").validate({ ignore: ".ignore" });'
  • @MohitBhardwaj 我也试过删除隐藏的类,但还是不行,我也是这么想的:(

标签: javascript jquery html validation upload


【解决方案1】:

试试这个:

...
qr_code_image: {
  required: true,
  accept: "image/jpeg, image/pjpeg"
}

【讨论】:

  • accept / extension 并不是真正的问题,即使 require 也行不通。
  • 请检查您在该页面 html 中的代码。也许您在其他 div(元素)上使用 qr_code_image 作为类或 id。
【解决方案2】:
$(document).ready(function () {

    $('#form').validate({
        ignore: [],
        rules: {
           city: {
                required: true
            },
            phone: {
                required: true,
            },
            qr_code_image: {
                required: true,
                accept: 'jpeg|png|jpeg'
            }
        },
         // Where to place the error message
        errorPlacement: function (error, element) {
            console.log(element);
            error.appendTo(element.next('.errorMsg'));
        }
    });

});

在选项中添加ignore:[] 字段可能会解决问题。我也遇到了类似的问题,解决了问题。

【讨论】:

  • 不走运 :( 忽略字段有什么作用?而且日子越来越奇怪...接受字段不会让我接受另一种格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多