0

我的表单设置了一些 HTML5,要求在提交之前填写字段,但由于 < IE10 不支持,我需要确保这些浏览器不允许提交空表单。 .

<form name="upload" method="post" action="send_form_email3.php" enctype="multipart/form-data">
    <div width="100%" class="con3">
        <div class="lable">
            <label for="first_name">First Name *</label>
        </div>
        <input  type="text" name="first_name" id="first_name" class="span4" required />

        <div class="lable">
            <label for="email">Email Address *</label>
        </div>
        <input  type="text" name="email" id="email" class="span4" required />

        <div class="lable">
            <label for="telephone">Contact Number *</label>
        </div>
        <input  type="text" name="telephone" id="telephone" class="span4" required />

        <div class="lable">
            <label for="comments">Why are you the fun, energetic person that we are looking for? *</label>
        </div>
        <textarea  name="comments" rows="8" id="comments" class="span4" required /></textarea>

        <div class="lable">
            <label for="upload">Send Us Your CV *</label>
        </div>
        <input type="file" name="upload" id="upload" required />

        <input type="submit" value="Submit" class="btn btn-success">
    </div>
</form>

为了在 < IE10 上处理此验证(还有其他不兼容 DOM 的浏览器吗?哈哈),我写了一些 jquery。

大多数情况下,以下内容适用于其他形式,但这次我想我可能会尝试对其进行一些改进并循环使用控件,.each而不是为每个输入运行一个新的 if 语句。

$("form[name=upload]").submit(function() {
    // Internet Explorer uses a different call to stop form submission
    // so we need to determine which browser is being used.

    // DOM compliant browsers use HTML5 to do this.

    if ($.browser.msie)
    {
        ($("form[name=upload] input").each(function() {
            if ($(this).val() == "")
            {
                var target, errmsg;
                switch ($(this).attr("name"))
                {
                    case "first_name":
                        errmsg += "Please enter your name.\r\n";
                        break;
                    case "email":
                        errmsg += "Please enter your email address.\r\n";
                        break;
                    case "telephone":
                        errmsg += "Please enter a contact number.\r\n";
                        break;
                    case "comments":
                        errmsg += "Please tell us why you are the fun, energetic person that we are looking for.\r\n";
                        break;
                    case "upload":
                        errmsg += "Please upload your CV so that we can review it.";
                        break;
                }
                event.returnValue = false;
                alert(errmsg);
            }
        });
    }
});

我不是很熟悉,.each()所以我不能确定自己是否正确。目前,此代码仍然允许表单提交通过,即使有空字段也不会发出任何警报。

4

2 回答 2

1

以下代码似乎有效:

  • 用括号和花括号修复了一些语法错误(textarea 的标记也有双重关闭'/')
  • 将自 jquery 1.9 以来不受支持的 $.browser.msie 替换为navigator.userAgent.match(/msie/i)(测试对required属性的支持可能会更好)

    $("form[name=upload]").submit(function() {      
    // Internet Explorer uses a different call to stop form submission
    // so we need to determine which browser is being used.
    
    // DOM compliant browsers use HTML5 to do this.
    
    if (navigator.userAgent.match(/msie/i))
    {
        $("form[name=upload] input").each(function() {
            if ($(this).val() == "")
            {
                var target, errmsg;
                switch ($(this).attr("name"))
                {
                    case "first_name":
                        errmsg += "Please enter your name.\r\n";
                        break;
                    case "email":
                        errmsg += "Please enter your email address.\r\n";
                        break;
                    case "telephone":
                        errmsg += "Please enter a contact number.\r\n";
                        break;
                    case "comments":
                        errmsg += "Please tell us why you are the fun, energetic person that we are looking for.\r\n";
                        break;
                    case "upload":
                        errmsg += "Please upload your CV so that we can review it.";
                        break;
                }
                event.returnValue = false;
                alert(errmsg);
            }
        });
        }
    });
    

希望这会有所帮助

于 2013-02-28T14:13:13.590 回答
0

所以最后我采取了简单的方法并用条件语句包围了我的脚本块:

<!--[if IE]>
<script type="text/javascript">
    // Do jquery validation here...
</script>
<![endif]-->
于 2013-03-11T07:58:56.887 回答