📅  最后修改于: 2023-12-03 14:43:28.358000             🧑  作者: Mango
jQWidgets jqxValidator 是一个表单验证插件,用于检查表单中的输入是否符合要求。onSuccess 属性是在表单成功验证后触发的函数,可以执行自定义操作,如禁用提交按钮、显示成功信息、跳转页面等。
在 HTML 文件中引入 jQWidgets 和 jQWidgets jqxValidator 库:
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jqwidgets.com/jqwidgets-ver10.1.5/jqxcore.js"></script>
<script type="text/javascript" src="https://cdn.jqwidgets.com/jqwidgets-ver10.1.5/jqxvalidator.js"></script>
</head>
在 HTML 文件中定义表单,并设置验证规则:
<form id="myForm">
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
</div>
<button type="submit" id="submitBtn">提交</button>
</form>
$(document).ready(function () {
$('#myForm').jqxValidator({
rules: [
{ input: '#name', message: '姓名不能为空!', action: 'keyup, blur', rule: 'required' },
{ input: '#email', message: '邮箱格式不正确!', action: 'keyup, blur', rule: 'email' }
]
});
});
在 JavaScript 文件中设置 onSuccess 函数:
$(document).ready(function () {
$('#myForm').jqxValidator({
rules: [
// ...
],
onSuccess: function () {
$('#submitBtn').prop('disabled', true);
alert('表单提交成功!');
window.location.href = '/success.html';
}
});
});
完整代码示例:
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jqwidgets.com/jqwidgets-ver10.1.5/jqxcore.js"></script>
<script type="text/javascript" src="https://cdn.jqwidgets.com/jqwidgets-ver10.1.5/jqxvalidator.js"></script>
</head>
<body>
<form id="myForm">
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
</div>
<button type="submit" id="submitBtn">提交</button>
</form>
<script>
$(document).ready(function () {
$('#myForm').jqxValidator({
rules: [
{ input: '#name', message: '姓名不能为空!', action: 'keyup, blur', rule: 'required' },
{ input: '#email', message: '邮箱格式不正确!', action: 'keyup, blur', rule: 'email' }
],
onSuccess: function () {
$('#submitBtn').prop('disabled', true);
alert('表单提交成功!');
window.location.href = '/success.html';
}
});
});
</script>
</body>
|参数名|类型|默认值|说明| |---|---|---|---| |onSuccess|function|null|表单成功验证后触发的函数| |rules|array|[]|验证规则数组,每个元素为一个验证规则对象|
无参数。