当其他浏览器进行验证时,是什么导致 IE 无法验证(jquery)

发布于 2024-12-14 07:32:12 字数 1631 浏览 1 评论 0 原文

我有一个使用 jQuery 1.7 和 jQuery 验证 1.9 的表单(在本文发布时都是最新的),它适用于 firefox / chrome / safari,但仅部分适用于 IE(至少 8.0,尚未测试其他版本) - - 不确定这笔交易是什么。 这里有一个例子: http://jsfiddle.net/bulbous/hZn5A/100/

如果您单击测试,您看到文本输入控件在所有浏览器中都经过验证,但下拉列表在 ie 中未经过验证(但在所有其他浏览器中均经过验证)。 我还包含了下面示例的完整 html:

<head>
    <script src="jquery-1.7.js" type="text/javascript"></script>
    <script src="jquery.validate-1.9.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
  jQuery.validator.addMethod("notEqual", function(value, element, param) {
      return this.optional(element) || value != param;
  }, "Please choose some value!");

  $('#myForm').validate({
      rules: {
          text: {
              required: true
          },
          category: {
              required: true,
              notEqual: "---"
          }
      },
      messages: {
          text: {
              required: "Text required"
          },
          category: {
              required: "Category required"
          }
      }
  });
});
    </script>
<form id="myForm">
    <select id="category" name="category">
        <option>---</option>
        <option>Category 1</option>
        <option>Category 2</option>
        <option>Category 3</option>
    </select>
    <input type="text" id="text" name="text" />
    <input type="submit" value="Test" />
</form
</body>
</html>

I have a form using jQuery 1.7 and jQuery validation 1.9 (latest each at the time of this post), which works on firefox / chrome / safari, but only partly works on I.E. (8.0 at least, haven't tested other versions) -- not sure what the deal is.
There's an example here:
http://jsfiddle.net/bulbous/hZn5A/100/

if you click Test, you see that the text input control is validated in all browsers, but the dropdown isn't validated in i.e. (but is in all the others).
I'm including the full html for the example below also:

<head>
    <script src="jquery-1.7.js" type="text/javascript"></script>
    <script src="jquery.validate-1.9.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
  jQuery.validator.addMethod("notEqual", function(value, element, param) {
      return this.optional(element) || value != param;
  }, "Please choose some value!");

  $('#myForm').validate({
      rules: {
          text: {
              required: true
          },
          category: {
              required: true,
              notEqual: "---"
          }
      },
      messages: {
          text: {
              required: "Text required"
          },
          category: {
              required: "Category required"
          }
      }
  });
});
    </script>
<form id="myForm">
    <select id="category" name="category">
        <option>---</option>
        <option>Category 1</option>
        <option>Category 2</option>
        <option>Category 3</option>
    </select>
    <input type="text" id="text" name="text" />
    <input type="submit" value="Test" />
</form
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眉黛浅 2024-12-21 07:32:12

如果您给 标记显式的“value”属性,它就会起作用。

<option value='---'>---</option>

我不清楚为什么它在 IE7 中失败但在 IE8 中却失败。

If you give the <option> tags explicit "value" attributes, it works.

<option value='---'>---</option>

It's not clear to me why it fails in IE7 but not IE8.

花想c 2024-12-21 07:32:12

似乎是验证器错误。但作为解决方法,您可以考虑选择元素并获取值。请参阅此,在 IE7 上运行:

jQuery.validator.addMethod("notEqual", function(value, element, param) {
    return this.optional(element) || $(element).val() !== param;
}, "Please choose some value!");

Seems to be a validator bug. But as a work around, you may consider select the element and get the value. See this, working on IE7:

jQuery.validator.addMethod("notEqual", function(value, element, param) {
    return this.optional(element) || $(element).val() !== param;
}, "Please choose some value!");
软的没边 2024-12-21 07:32:12

这是因为你的选择都没有价值。

    <option value='---'>---</option>
    <option value='Category 1'>Category 1</option>
    <option value='Category 2'>Category 2</option>
    <option value='Category 3'>Category 3</option>

It's because none of your options have values.

    <option value='---'>---</option>
    <option value='Category 1'>Category 1</option>
    <option value='Category 2'>Category 2</option>
    <option value='Category 3'>Category 3</option>
爱的十字路口 2024-12-21 07:32:12

您的 idname 使用“text”一词。

“文本”一词并不是专门“保留”,但它是“最好避免使用的词”之一...

http://www.kourbatov.com/faq/reserved.htm


此外,验证插件仅经过测试至 jQuery 版本 1.6.1

只需降级您的 jQuery 版本排除 jQuery 1.7 的问题

You're using the word "text" for your id and name.

The word "text" is not specifically "reserved" but it's one of those "words best to avoid"...

http://www.kourbatov.com/faq/reserved.htm


Also, the Validation plugin has only been tested up to jQuery version 1.6.1

Downgrade your jQuery version just to rule out an issue with jQuery 1.7

冷了相思 2024-12-21 07:32:12

猜猜这应该有用

<html>
<head>
    <script src="jquery-1.7.js" type="text/javascript"></script>
    <script src="jquery.validate-1.9.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
    $('#myForm').validate({
      rules: {
          text: {
              required: true
          },
          category: {
              required: true
          }
      },
      messages: {
          text: {
              required: "Text required"
          },
          category: {
              required: "Category required"
          }
      }
  });
});
    </script>
<form id="myForm">
    <select id="category" name="category">
        <option value="">---</option>
        <option value="Category 1">Category 1</option>
        <option value="Category 2">Category 2</option>
        <option value="Category 3">Category 3</option>
    </select>
    <input type="text" id="text" name="text" />
    <input type="submit" value="Test" />
</form
</body>
</html>

guess this should work

<html>
<head>
    <script src="jquery-1.7.js" type="text/javascript"></script>
    <script src="jquery.validate-1.9.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
    $('#myForm').validate({
      rules: {
          text: {
              required: true
          },
          category: {
              required: true
          }
      },
      messages: {
          text: {
              required: "Text required"
          },
          category: {
              required: "Category required"
          }
      }
  });
});
    </script>
<form id="myForm">
    <select id="category" name="category">
        <option value="">---</option>
        <option value="Category 1">Category 1</option>
        <option value="Category 2">Category 2</option>
        <option value="Category 3">Category 3</option>
    </select>
    <input type="text" id="text" name="text" />
    <input type="submit" value="Test" />
</form
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文