jQuery应验框架(三、四)选择器及实用工具 (jQuery validation)
jQuery验证框架
三、定制的选择器(Custom Selectors)
[1] :blank 返回:Array<Element>
说明:匹配所有空值的表单元素。没有任何值或都空白符(whitespace)都被认为是空值。
它是由 jQuery.trim(value).length == 0 来判断的。
- $("input:blank").css("background-color", "#bbbbff");
$("input:blank").css("background-color", "#bbbbff");
[2] :filled 返回:Array<Element>
说明:匹配所有不为空的表单元素。任何值都可以认为是已输入的,但只有空白符的值除外。
它是由 jQuery.trim(value).length > 0 来判断的。
- $("input:filled").css("background-color", "#bbbbff");
$("input:filled").css("background-color", "#bbbbff");
[3] :unchecked 返回:Array<Element>
说明:匹配所有未选择的表单元素。反向操作为 :checked
- function countUnchecked() {
- var n = $("input:unchecked").length;
- $("div").text(n + (n == 1 ? " is" : " are") + " unchecked!");
- }
- countUnchecked();
- $(":checkbox").click(countUnchecked);
function countUnchecked() { var n = $("input:unchecked").length; $("div").text(n + (n == 1 ? " is" : " are") + " unchecked!"); } countUnchecked(); $(":checkbox").click(countUnchecked);
四、实用工具(Utilities)
jQuery.validator.format( template, [argument], [argumentN...] ) 返回:String
参数 template 类型:String 要格式化的字符串
参数 argument (Optional) 类型:String, Array<String> 用字符串或字符串数组(对应索引的元素)填充第一个占位符
参数 argumentN... (Optional) 类型:String 填充第二个或之后的占位符。
说明:用参数来填充{n}占位符。除template外的一个或多个参数都可以用来填充相应的占位符。
- $(document).ready(function(){
- $("button").click(function () {
- var str = "Hello {0}, this is {1}";
- alert("'" + str + "'");
- str = jQuery.validator.format(str, "World", "Bob");
- //str = $.validator.format(str, ["koala","oo"]);
- alert("'" + str + "'");
- });
- });
$(document).ready(function(){ $("button").click(function () { var str = "Hello {0}, this is {1}"; alert("'" + str + "'"); str = jQuery.validator.format(str, "World", "Bob"); //str = $.validator.format(str, ["koala","oo"]); alert("'" + str + "'"); }); });
原文请见:http://docs.jquery.com/Plugins/Validation
--------------------------------------------------------------------------------------