【问题标题】:jQuery validation not working with Chrome or IE8jQuery 验证不适用于 Chrome 或 IE8
【发布时间】:2014-03-11 22:00:48
【问题描述】:

我正在尝试使用 jQuery 验证,但它在提交时没有验证。如果我删除规则,并将 required 添加到 firstName 字段,它将在 Chrome 下工作,但在 IE8 下不行。就目前而言,它无法在 Chrome 或 IE8 下运行。我知道 IE8 有它的怪癖,但不幸的是我的客户需要 IE8,但我显然遗漏了一些东西。有什么建议吗?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Validation Test</title>

    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery.validate.min.js"></script> 

<script type="text/javascript">
$(function(){        
    $("#main_form").validate({
        rules: {
            firstName: {
                required: true
            }
        }
    })
});
</script>
</head>
<body>
    <form id="main_form">
        <div>
            <label for="firstName">First Name</label>
            <input type="text" id="firstName">
        </div>
        <div>
            <label for="lastName">Last Name</label>
            <input type="text" id="lastName">
        </div>
        <div>
           <label for="address1">Address 1</label>
            <input type="text" id="address1">
        </div>
        <div>
            <label for="address2">Address 2</label>
            <input type="text" id="address2">
        </div> 
        <div>
            <label for="cityState">City/State</label>
            <input type="text" id="cityState">
        </div>
        <div>
            <button type="submit">Search</button>    
        </div>          
     </form>
</body>
</html>

【问题讨论】:

  • 首先,请将该代码包装在jQuery(function($) { // code in here }); 中。有些浏览器在解析输出之前会缓冲输出,有些则不会。如果没有,那么您的代码在您的表单甚至存在于 DOM 之前执行
  • 接下来,你是如何测试IE8的?使用“模拟器”通常是不可靠的,最好的方法是使用带有 IE 的成熟虚拟机,请参阅modern.ie/en-us。 (我假设您没有安装 IE8 作为默认 IE 版本)。
  • 我安装了 IE8 作为我的默认浏览器。正如我所说,我的客户需要 IE8。但是,上面的代码在 Chrome 中也不起作用。
  • 我编辑了代码以将其包装在 jQuery 函数中($)(见上文)相同的结果 ...

标签: jquery validation internet-explorer-8 jquery-validate


【解决方案1】:

查看 jquery-validate 标记的 faq

在声明时,规则由输入名称属性而非 id 定义 在 validate() 的规则选项中。

因此,您需要为每个输入字段添加name 属性,如下所示:

 <input type="text" id="firstName" name="firstName">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2013-10-23
    • 2012-01-12
    相关资源
    最近更新 更多