我的表单设置了一些 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()
所以我不能确定自己是否正确。目前,此代码仍然允许表单提交通过,即使有空字段也不会发出任何警报。