提交 | 用户 | 时间
|
58d006
|
1 |
<!DOCTYPE html> |
A |
2 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
3 |
<head> |
|
4 |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> |
|
5 |
<title>jQuery validation plug-in - dynamic forms demo</title> |
|
6 |
|
|
7 |
<link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" /> |
|
8 |
|
|
9 |
<script src="../lib/jquery.js" type="text/javascript"></script> |
|
10 |
<script src="../jquery.validate.js" type="text/javascript"></script> |
|
11 |
|
|
12 |
<script type="text/javascript"> |
|
13 |
// only for demo purposes |
|
14 |
$.validator.setDefaults({ |
|
15 |
submitHandler: function() { |
|
16 |
alert("submitted!"); |
|
17 |
} |
|
18 |
}); |
|
19 |
$.validator.messages.max = jQuery.validator.format("Your totals mustn't exceed {0}!"); |
|
20 |
|
|
21 |
$.validator.addMethod("quantity", function(value, element) { |
|
22 |
return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]); |
|
23 |
}, "Please select both the item and its amount."); |
|
24 |
|
|
25 |
$().ready(function() { |
|
26 |
$("#orderform").validate({ |
|
27 |
errorPlacement: function(error, element) { |
|
28 |
error.appendTo( element.parent().next() ); |
|
29 |
}, |
|
30 |
highlight: function(element, errorClass) { |
|
31 |
$(element).addClass(errorClass).parent().prev().children("select").addClass(errorClass); |
|
32 |
} |
|
33 |
}); |
|
34 |
|
|
35 |
var template = jQuery.validator.format($.trim($("#template").val())); |
|
36 |
function addRow() { |
|
37 |
$(template(i++)).appendTo("#orderitems tbody"); |
|
38 |
} |
|
39 |
|
|
40 |
var i = 1; |
|
41 |
// start with one row |
|
42 |
addRow(); |
|
43 |
// add more rows on click |
|
44 |
$("#add").click(addRow); |
|
45 |
|
|
46 |
// check keyup on quantity inputs to update totals field |
|
47 |
$("#orderform").validateDelegate("input.quantity", "keyup", function(event) { |
|
48 |
var totals = 0; |
|
49 |
$("#orderitems input.quantity").each(function() { |
|
50 |
totals += +this.value; |
|
51 |
}); |
|
52 |
$("#totals").attr("value", totals).valid(); |
|
53 |
}); |
|
54 |
|
|
55 |
}); |
|
56 |
</script> |
|
57 |
|
|
58 |
<style type="text/css"> |
|
59 |
form.cmxform { width: 50em; } |
|
60 |
em.error { |
|
61 |
background:url("images/unchecked.gif") no-repeat 0px 0px; |
|
62 |
padding-left: 16px; |
|
63 |
} |
|
64 |
em.success { |
|
65 |
background:url("images/checked.gif") no-repeat 0px 0px; |
|
66 |
padding-left: 16px; |
|
67 |
} |
|
68 |
|
|
69 |
form.cmxform label.error { |
|
70 |
margin-left: auto; |
|
71 |
width: 250px; |
|
72 |
} |
|
73 |
form.cmxform input.submit { |
|
74 |
margin-left: 0; |
|
75 |
} |
|
76 |
em.error { color: black; } |
|
77 |
#warning { display: none; } |
|
78 |
select.error { |
|
79 |
border: 1px dotted red; |
|
80 |
} |
|
81 |
</style> |
|
82 |
|
|
83 |
</head> |
|
84 |
<body> |
|
85 |
|
|
86 |
<h1 id="banner"><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation Plugin</a> Demo</h1> |
|
87 |
<div id="main"> |
|
88 |
|
|
89 |
<textarea style="display:none" id="template"> |
|
90 |
<tr> |
|
91 |
<td> |
|
92 |
<label>{0}. Item</label> |
|
93 |
</td> |
|
94 |
<td class='type'> |
|
95 |
<select name="item-type-{0}"> |
|
96 |
<option value="">Select...</option> |
|
97 |
<option value="0">Learning jQuery</option> |
|
98 |
<option value="1">jQuery Reference Guide</option> |
|
99 |
<option value="2">jQuery Cookbook</option> |
|
100 |
<option vlaue="3">jQuery In Action</option> |
|
101 |
<option value="4">jQuery For Designers</option> |
|
102 |
</select> |
|
103 |
</td> |
|
104 |
<td class='quantity'> |
|
105 |
<input size='4' class="quantity" min="1" id="item-quantity-{0}" name="item-quantity-{0}" /> |
|
106 |
</td> |
|
107 |
<td class='quantity-error'></td> |
|
108 |
</tr> |
|
109 |
</textarea> |
|
110 |
|
|
111 |
<form id="orderform" class="cmxform" method="get" action="foo.html"> |
|
112 |
<h2 id="summary"></h2> |
|
113 |
|
|
114 |
<fieldset> |
|
115 |
<legend>Example with custom methods and heavily customized error display</legend> |
|
116 |
<table id="orderitems"> |
|
117 |
<tbody> |
|
118 |
|
|
119 |
</tbody> |
|
120 |
<tfoot> |
|
121 |
<tr> |
|
122 |
<td colspan="2"><label>Totals (max 25)</label></td> |
|
123 |
<td class="totals"><input id="totals" name="totals" value="0" max="25" readonly="readonly" size='4' /></td> |
|
124 |
<td class="totals-error"></td> |
|
125 |
</tr> |
|
126 |
<tr> |
|
127 |
<td colspan="2"> </td> |
|
128 |
<td><input class="submit" type="submit" value="Submit"/></td> |
|
129 |
</tr> |
|
130 |
</tfoot> |
|
131 |
</table> |
|
132 |
</fieldset> |
|
133 |
</form> |
|
134 |
|
|
135 |
<button id="add">Add another input to the form</button> |
|
136 |
|
|
137 |
<h1 id="warning">Your form contains tons of errors! Please try again.</h1> |
|
138 |
|
|
139 |
<p><a href="index.html">Back to main page</a></p> |
|
140 |
|
|
141 |
</div> |
|
142 |
|
|
143 |
|
|
144 |
</body> |
|
145 |
</html> |