我必须使用 jquery 制作简单的验证表单,(没有 3rd 方插件)为此我编写了以下代码..
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<style>
.InvaildField {
border: 0.5px solid red;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnSubmit').attr("disabled", true);
$('.emptyField').blur(function () {
if ($(this).val() == '') {
$(this).removeAttr('class');
$(this).addClass('InvaildField');
$('#btnSubmit').attr("disabled", true);
} else {
$(this).removeAttr('class');
$('#btnSubmit').attr("disabled", false);
}
});
$('.emptyField').keyup(function () {
if ($(this).val() == '') {
$(this).removeAttr('class');
$(this).addClass('InvaildField');
$('#btnSubmit').attr("disabled", true);
} else {
$(this).removeAttr('class');
$('#btnSubmit').attr("disabled", false);
}
});
});
</script>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td><input class="emptyField" type="text"/></td>
</tr>
<tr>
<td>Email</td>
<td><input class="emptyField" type="text"/></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input class="emptyField" type="text"/></td>
</tr>
<tr>
<td colspan="2">
<input id="btnSubmit" class="emptyField" type="button" value="Submit"/>
</td>
</tr>
</table>
</body>
</html>
但问题是当我填充第一个文本框时,按钮已启用,当所有 texboxes 不为空时如何启用按钮,