I have the following code: http://jsfiddle.net/stomp/a8vjg/
Everything works well except I need the name of the select field to be extras[0], then extras[1] for the next one added and so on..
Can anybody help?
Cheers, Steve
I have the following code: http://jsfiddle.net/stomp/a8vjg/
Everything works well except I need the name of the select field to be extras[0], then extras[1] for the next one added and so on..
Can anybody help?
Cheers, Steve
Do you mean:
case "add":
var newTag = tag.clone();
var i = $("#multi").find("select").length;
console.log(i);
newTag.find("select").attr("name", 'extras['+i+']');
tag.after(newTag.find("input").val("").end());
break;
See: jsFiddle
You need sth like this when cloning:
case "add":
var cnt=jqEl.parent().siblings().size();
var cloned=tag.clone();
$(cloned).find("input").val("")
.end()
.find('select').attr('name','extra['+cnt+']');
tag.after(cloned);
break;
check the working fork (might be duplicates if you remove and add)
No duplicates fiddle (serial number could not be ensured if you delete a node)
So the perfect solution could be adding the ids on [Save] button here is the solution
function save(e) {
multiTags.find("select").each(function(i){
$(this).attr('name','extras['+i+']');
});
var tags = multiTags.find("input.tag").map(function() {
return $(this).val();
}).get().join(',');
alert(tags);
return false;
}