我的表单设置了一些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>
在&lt;处理此验证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()
不太熟悉,所以如果我做对了,我就无法确定自己。目前,此代码仍允许表单提交通过,即使有空字段也不会提醒任何内容。
答案 0 :(得分:1)
以下代码似乎有效:
替换了jquery 1.9与navigator.userAgent.match(/msie/i)
不支持的$ .browser.msie(测试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);
}
});
}
});
希望这会有所帮助
答案 1 :(得分:0)
所以最后我采取了简单的方法,用条件语句包围我的脚本块:
<!--[if IE]>
<script type="text/javascript">
// Do jquery validation here...
</script>
<![endif]-->