Skip to content

Instantly share code, notes, and snippets.

@wookiecooking
Created August 8, 2014 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookiecooking/968c589946c8973cb013 to your computer and use it in GitHub Desktop.
Save wookiecooking/968c589946c8973cb013 to your computer and use it in GitHub Desktop.
// 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();
})
.template {display:none};
<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