我没有成功创建自定义 jQuery 验证规则
I have no success to create my custom jQuery Validation Rule
我想为 jqGrid table 创建自己的验证规则,但我的代码不起作用。为什么?
这是HTML:
<form id="frmTCstepCategorie">
<fieldset class="ui-widget ui-widget-content">
<legend class="ui-widget">
Categorie
</legend>
<table id="tableCategories" name="tableCategories">
<tr>
<td></td>
</tr>
</table>
<div style="margin-top: 10px" class="categoriesNav">
<input type="button" id="btnAddCategory" value="Aggiungi"/>
<input type="button" id="btnDelCategory" value="Rimuovi"/>
</div>
</fieldset>
</form>
这是 JavaScript 代码:
$.validator.addMethod("categories", function(value, element) {
console.log("Checking...");
return true;
}, "* Required.");
$("#frmTCstepCategorie").validate({
rules : {
"tableCategories" : {
categories : true
}
}
});
if ($("#frmTCstepCategorie").valid()) {
console.log("Checked!");
}
更新
@Sparky 告诉我正确的事情:
Using the jQuery Validate plugin, you can only validate <input>
, <select>
, <textarea>
elements.
Programmatically, you could copy the data into a hidden input and validate that element instead. You would need to set the ignore
option to [] in order to validate hidden elements, and maybe the errorPlacement
option to put the messages where you need them.
我尝试这样做,但没有成功。
如果我添加一个隐藏的<input>
:
<input type="hidden" id="hiddenCategories" name="hiddenCategories" />
并且我修改了忽略参数:
$("#frmTCstepCategorie").validate({
rules : {
"hiddenCategories" : {
categories : true,
ignore: []
}
}
});
没用。
如果我将 hidden
更改为 text
:
<input type="text" id="hiddenCategories" name="hiddenCategories" />
有效
哪里错了?
I'd like to create my own validation rule for a jqGrid table, but my code does not work. Why?
在您的代码中,您试图将验证规则分配给 <table>
元素。
使用 jQuery 验证插件,您只能验证 <input>
、<select>
、<textarea>
元素。无论您使用什么规则,标准的还是自定义的,这些都是唯一可行的元素,并且 没有解决方法 可以让您验证 <table>
或 <td>
元素。
以编程方式,您可以将数据复制到隐藏的 input
中并改为验证该元素。您需要设置 the ignore
option to []
in order to validate hidden elements, and maybe the errorPlacement
option 以将消息放在您需要的地方。
编辑:
$("#frmTCstepCategorie").validate({
ignore: [], // <- ignore is an OPTION, not a rule
rules : {
"hiddenCategories" : {
categories : true
}
}
});
我想为 jqGrid table 创建自己的验证规则,但我的代码不起作用。为什么?
这是HTML:
<form id="frmTCstepCategorie">
<fieldset class="ui-widget ui-widget-content">
<legend class="ui-widget">
Categorie
</legend>
<table id="tableCategories" name="tableCategories">
<tr>
<td></td>
</tr>
</table>
<div style="margin-top: 10px" class="categoriesNav">
<input type="button" id="btnAddCategory" value="Aggiungi"/>
<input type="button" id="btnDelCategory" value="Rimuovi"/>
</div>
</fieldset>
</form>
这是 JavaScript 代码:
$.validator.addMethod("categories", function(value, element) {
console.log("Checking...");
return true;
}, "* Required.");
$("#frmTCstepCategorie").validate({
rules : {
"tableCategories" : {
categories : true
}
}
});
if ($("#frmTCstepCategorie").valid()) {
console.log("Checked!");
}
更新
@Sparky 告诉我正确的事情:
Using the jQuery Validate plugin, you can only validate
<input>
,<select>
,<textarea>
elements.Programmatically, you could copy the data into a hidden input and validate that element instead. You would need to set the
ignore
option to [] in order to validate hidden elements, and maybe theerrorPlacement
option to put the messages where you need them.
我尝试这样做,但没有成功。
如果我添加一个隐藏的<input>
:
<input type="hidden" id="hiddenCategories" name="hiddenCategories" />
并且我修改了忽略参数:
$("#frmTCstepCategorie").validate({
rules : {
"hiddenCategories" : {
categories : true,
ignore: []
}
}
});
没用。
如果我将 hidden
更改为 text
:
<input type="text" id="hiddenCategories" name="hiddenCategories" />
有效
哪里错了?
I'd like to create my own validation rule for a jqGrid table, but my code does not work. Why?
在您的代码中,您试图将验证规则分配给 <table>
元素。
使用 jQuery 验证插件,您只能验证 <input>
、<select>
、<textarea>
元素。无论您使用什么规则,标准的还是自定义的,这些都是唯一可行的元素,并且 没有解决方法 可以让您验证 <table>
或 <td>
元素。
以编程方式,您可以将数据复制到隐藏的 input
中并改为验证该元素。您需要设置 the ignore
option to []
in order to validate hidden elements, and maybe the errorPlacement
option 以将消息放在您需要的地方。
编辑:
$("#frmTCstepCategorie").validate({
ignore: [], // <- ignore is an OPTION, not a rule
rules : {
"hiddenCategories" : {
categories : true
}
}
});