Skip to content

Commit 2bfcb5d

Browse files
author
Maxim Vasiliev
committed
Initial commit
0 parents  commit 2bfcb5d

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

example/index.html

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<!doctype html>
2+
3+
<!--[if lt IE 7 ]> <html lang="ru" class="no-js ie6"> <![endif]-->
4+
<!--[if IE 7 ]> <html lang="ru" class="no-js ie7"> <![endif]-->
5+
<!--[if IE 8 ]> <html lang="ru" class="no-js ie8"> <![endif]-->
6+
<!--[if IE 9 ]> <html lang="ru" class="no-js ie9"> <![endif]-->
7+
<!--[if (gt IE 9)|!(IE)]><!-->
8+
<html lang="ru" class="no-js"> <!--<![endif]-->
9+
<head>
10+
<meta charset="utf-8">
11+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12+
</head>
13+
14+
<body>
15+
<h1>jQuery.deserialize example</h1>
16+
<h2>Test links</h2>
17+
<ul>
18+
<li><a href="#!txtString=Test+string+1&pwdPassword=passwd&rbRadio=1&selSelect=1&hidden=no+hidden+values">#!txtString=Test+string+1&pwdPassword=passwd&rbRadio=1&selSelect=1&hidden=no+hidden+values</a></li>
19+
<li><a href="#!txtString=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D0%BC%D0%B8%D1%80!&pwdPassword=%D0%BA%D0%BE%D0%B5+%D1%87%D0%B5%D0%B3%D0%BE+%D0%B5%D1%89%D0%B5&chkCheckbox=test&rbRadio=2&selSelect=2.1&hidden=">#!txtString=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D0%BC%D0%B8%D1%80!&pwdPassword=%D0%BA%D0%BE%D0%B5+%D1%87%D0%B5%D0%B3%D0%BE+%D0%B5%D1%89%D0%B5&chkCheckbox=test&rbRadio=2&selSelect=2.1&hidden=</a></li>
20+
</ul>
21+
<h2>Form</h2>
22+
<form id="testForm">
23+
<dl>
24+
<dt><label for="txtString">Text field</label></dt>
25+
<dd><input id="txtString" name="txtString" type="text" value="Hello world!" /></dd>
26+
</dl>
27+
28+
<dl>
29+
<dt><label for="pwdPassword">Password field</label></dt>
30+
<dd><input id="pwdPassword" name="pwdPassword" type="password" value="password!" /></dd>
31+
</dl>
32+
33+
<dl>
34+
<dd><label><input name="chkCheckbox" type="checkbox" value="test" checked="checked" /> checkbox</label></dd>
35+
</dl>
36+
37+
<dl>
38+
<dt>Radiobuttons</dt>
39+
<dd>
40+
<ul>
41+
<li><label><input name="rbRadio" type="radio" value="1" /> radio 1</label></li>
42+
<li><label><input name="rbRadio" type="radio" value="2" checked="checked" /> radio 2</label></li>
43+
<li><label><input name="rbRadio" type="radio" value="3" /> radio 3</label></li>
44+
<li><label><input name="rbRadio" type="radio" value="4" /> radio 4</label></li>
45+
</ul>
46+
</dd>
47+
</dl>
48+
49+
<dl>
50+
<dt><label for="selSelect">Select</label></dt>
51+
<dd>
52+
<select id="selSelect" name="selSelect">
53+
<option value="1">option 1</option>
54+
<optgroup label="option 2">
55+
<option value="2.1" selected="selected">option 2.1</option>
56+
<option value="2.2">option 2.2</option>
57+
<option value="2.3">option 2.3</option>
58+
</optgroup>
59+
<option value="3">option 3</option>
60+
<option value="4">option 4</option>
61+
<option value="5">option 5</option>
62+
</select>
63+
</dd>
64+
</dl>
65+
66+
<dl>
67+
<dt><label for="txtTextarea">Textarea</label></dt>
68+
<dd><textarea id="txtTextarea">Hello world again!</textarea></dd>
69+
</dl>
70+
71+
<dl>
72+
<dd><button type="submit">Serialize form</button></dd>
73+
</dl>
74+
75+
<input type="hidden" name="hidden" value="test" />
76+
</form>
77+
78+
<h2>Serialized data</h2>
79+
<p id="serializedData"></p>
80+
81+
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
82+
<script type="text/javascript" src="../src/jquery.deserialize.js"></script>
83+
<script type="text/javascript">
84+
$(function(){
85+
$('button[type=submit]').click(function(){
86+
$('#serializedData').html($('#testForm').serialize());
87+
return false;
88+
});
89+
90+
$('a[href^="#!"]').click(function(evt){
91+
var hashBang = /#!(.*)$/.exec(this.href),
92+
data = hashBang[1] || '';
93+
94+
$('#testForm').deserialize(data, true);
95+
96+
if (data == $('#testForm').serialize())
97+
alert('success!');
98+
else
99+
alert('failure!');
100+
101+
return false;
102+
});
103+
});
104+
</script>
105+
</body>
106+
</html>

example/jquery-1.6.1.min.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.deserialize.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @author Maxim Vasiliev
3+
* Date: 21.01.2010
4+
* Time: 14:00
5+
*/
6+
7+
(function($)
8+
{
9+
/**
10+
* jQuery.deserialize plugin
11+
* Fills elements in selected containers with data extracted from URLencoded string
12+
* @param data URLencoded data
13+
* @param clearForm if true form will be cleared prior to deserialization
14+
*/
15+
$.fn.deserialize = function(data, clearForm)
16+
{
17+
this.each(function(){
18+
deserialize(this, data, !!clearForm);
19+
});
20+
};
21+
22+
/**
23+
* Fills specified form with data extracted from string
24+
* @param element form to fill
25+
* @param data URLencoded data
26+
* @param clearForm if true form will be cleared prior to deserialization
27+
*/
28+
function deserialize(element, data, clearForm)
29+
{
30+
var splits = decodeURIComponent(data).split('&'),
31+
i = 0,
32+
split = null,
33+
key = null,
34+
value = null,
35+
splitParts = null;
36+
37+
if (clearForm)
38+
{
39+
$('input[type="checkbox"],input[type="radio"]', element).removeAttr('checked');
40+
$('select,input[type="text"],input[type="password"],input[type="hidden"],textarea', element).val('');
41+
}
42+
43+
while(split = splits[i++])
44+
{
45+
splitParts = split.split('=');
46+
key = splitParts[0] || '';
47+
value = (splitParts[1] || '').replace(/\+/g, ' ');
48+
49+
if (key != '')
50+
{
51+
$('input[type="checkbox"][name="'+ key +'"][value="'+ value +'"],input[type="radio"][name="'+ key +'"][value="'+ value +'"]', element).attr('checked', 'checked');
52+
$('select[name="'+ key +'"],input[type="text"][name="'+ key +'"],input[type="password"][name="'+ key +'"],input[type="hidden"][name="'+ key +'"],textarea[name="'+ key +'"]', element).val(value);
53+
}
54+
}
55+
}
56+
57+
})(jQuery);

0 commit comments

Comments
 (0)