我想用 jQuery 验证函数验证一个 jQuery UI 对话框,但是我遇到了一些问题。我想要一个简单的例子来工作,我验证输入字段的文本 - 该字段应该包含数据,它应该代表一个数字。请看下面的代码。我怎么能做到这一点?
<head>
<script src="../Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery-ui-1.9.2.custom.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<link href="../Styles/jquery-ui-1.9.2.custom.min.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#field { margin-left: .5em; float: left; }
#field, label { float: left; font-family: Arial, Helvetica, sans-serif; font-size: small; }
br { clear: both; }
input { border: 1px solid black; margin-bottom: .5em; }
input.error { border: 1px solid red; }
label.error {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/unchecked.gif') no-repeat;
padding-left: 16px;
margin-left: .3em;
}
label.valid {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/checked.gif') no-repeat;
display: block;
width: 16px;
height: 16px;
}
</style>
<script type="text/javascript">
var validationNumber = null;
function GetNumberValidator() {
return $("#FormToValidate").validate({
rules: {
TextToValidate: {
required: true,
digits: true
}
}
});
}
$(function () {
$("#button1").button();
$("#dialog-message").dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function () {
if (validationNumber.element("#TextToValidate")) {
$(this).dialog("close");
}
}
}
});
});
$().ready(function () {
$("#button1").click(function () {
$("#dialog-message").dialog("open");
}
);
validationNumber = GetNumberValidator();
});
</script>
<title>Dialog Number</title>
</head>
<body>
<button id="button1">A button element</button>
<form id="FormToValidate" action="DialogNumber.html" method="post">
<div id="dialog-message" title="Input">
<input id="TextToValidate" name="TextToValidate" type="text" />
<div id="ErrorMessage"></div>
</div>
</form>
</body>