Created
August 8, 2014 09:36
-
-
Save wookiecooking/968c589946c8973cb013 to your computer and use it in GitHub Desktop.
key/value form2json - http://codepen.io/wookiecooking/pen/tphAF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// require('jquery') | |
$( "form" ).submit(function( event ) { | |
var ara = {} | |
$('form .set').each(function(){ | |
var data = $(this).find("input") | |
ara[data[0].value] = data[1].value; | |
}) | |
$('.result p').text(JSON.stringify(ara)) | |
if($('.list div').length >= 5) { | |
alert('to many to ajax'); | |
} | |
event.preventDefault(); | |
}); | |
// add new input set | |
$('.add').click(function(event){ | |
if($('.list div').length >= 5) { | |
return alert('to many'); | |
} | |
$('.template').clone(true) | |
.removeClass('template') | |
.appendTo( ".list" ); | |
event.preventDefault(); | |
}) | |
// remove input set | |
$('.set .remove').click(function(event){ | |
$(this).parent('.set').remove(); | |
event.preventDefault(); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.template {display:none}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="#" class="add">Add New</a> | |
<div class="template set"> | |
<input type="text" placeholder="key" name="key" value=""> | |
<input type="text" placeholder="value" name="value" value=""> | |
<a href="#" class='remove'> Remove</a> | |
</div> | |
<form> | |
<div class="list"> | |
<div class="set"> | |
<input type="text" placeholder="key" name="key" value=""> | |
<input type="text" placeholder="value" name="value" value=""> | |
<a href="#" class='remove'> Remove</a> | |
</div> | |
</div> | |
<button type="submit"> Click </button> | |
</form> | |
<div class="result"><h2>result</h2><p></p></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment