如果我的標題是正確的,但我目前有得到JSON字符串化的重複,能形式和其他正常的輸入端,獲取JSON字符串化,所以當克隆是我不安靜肯定點擊第二個輸入被克隆並點擊時提交被我得到了控制檯以下值:JSON對象上合併兩個JSON字符串化值到
{"dependant1":[{"name":"daniel"}],"dependant2":[{"name":"allen"}]}
{"mainmember":[{"name":"steve"}]}
我怎樣才能將二者結合起來,以獲得:
{"mainmember": [{"name": "steve"}],"dependant1": [{"name": "daniel"}],"dependant2": [{"name": "allen"}]}
繼承人的jsfiddle:http://jsfiddle.net/dawidvdh/uhJ7w/
,代碼:
的jQuery:
//Clone Tracking
var g_counter = 1;
var d_counter = 1;
var dependant = ["dependant"];
var input_groups = ["group-1"];
var group;
var name_input_groups = ["name-group-1"];
var values;
var name_fields=[0];
var name_input = "<input class='name' name='name' type='text' />";
jQuery(document).ready(function(e) {
jQuery(name_fields).each(function() {
jQuery(name_input).appendTo('#name-group-1');
});
jQuery('#clone').click(function() {
clone_dependant();
});
function clone_dependant() {
var oldId = g_counter;
g_counter++;
var id_newDiv = 'group-'+ g_counter;
currentdep ='dependant-'+g_counter;
var $clonedDiv = jQuery('#dependant-1').clone(false).attr('id', 'dependant-'+g_counter);
var name_newDiv = 'name-group-'+ g_counter;
$clonedDiv.find('#name-group-1').attr('id',"name-group-" + g_counter);
$clonedDiv.find('input[type="text"]').val('');
$clonedDiv.insertAfter("#dependant-" + oldId);
name_input_groups.push(name_newDiv);
input_groups.push(id_newDiv);
}
var result = {};
var dependants;
var mainmember;
var main_result = {};
var dep_counter = 0;
jQuery('#submit').click(function(){
jQuery('.dependant').each(function(k, v){
dep_counter++
dependants = {};
result['dependant'+dep_counter] = [dependants];
dependants['name'] = $(v).find('.name').val();
});
jQuery('.main-member').each(function(k, v){
mainmember = {}
mainmember['name'] = $(v).find('.main_name').val();
main_result['mainmember'] = [mainmember];
});
var jsonData = JSON.stringify(result);
var mainData = JSON.stringify(main_result);
console.log(jsonData);
console.log(mainData);
jQuery.ajax({
type: "POST",
url: "mail.php",
dataType: "json",
data: {parameters: jsonData}
});
});
//submit function
});
然後將HTML
<div id="main-member" class="main-member">
<div id="label">full name:</div> <input class="main_name" />
</div>
<div id="dependant-1" class="dependant">
<div id="label">full name:</div> <div id="name-group-1"></div>
</div>
<button id="clone">Add a Dependant</button>
<button id="submit">submit</button>
而且他們要在那裏擁有的每個循環的代碼的其餘工作。
在此先感謝:)。
看,你會喜歡它:) HTTP:// knockoutjs.com/ – whosrdaddy
'$ .extend'方法效果很好,但爲什麼不首先將'mainmember'聲明爲'main_result'對象的'key'? 'result ['mainmember'] = [mainmember];' – Ohgodwhy