0
此代碼是我的函數的一部分,它創建了selectbox並將索引放到每個選項值中。我的問題是如何將所有這些字段(textboxes和selectboxes)值添加到特定的輸入基於提交表單隱藏類型? 這裏是我的代碼:將所有表單字段值合併爲一個
$(function() {
var createChildDropdown = function(i) {
var $childDropdown = $('<div />', {
'class': 'childs'
});
$childDropdown.append($('<label />', {
'for': 'childDropdown-' + i
}).text('Child age ' + i));
$childDropdown.append($('<select />', {
'name': 'childDropdown'
}));
var options = [' 1 years old', '2 years old', '3 years old'];
options.forEach(function(option, index) {
\t $childDropdown.find('select').append($('<option />').text(option).attr('value', `,${index}`));
});
return $childDropdown;
};
var destroyChildDropdown = function($el, i) {
$el.find('div.childs').get(i).remove();
};
$(".button-click-child a").on("click", function() {
var button = $(this);
var oldVal = parseInt(button.closest("ul").prev().val());
var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
var total_value = "";
\t if(newVal >= 5) return;
button.closest("ul").prev().val(newVal);
$(".cat_textbox").each(function() {
var cat = $(this).prev('span').text();
});
if (oldVal < newVal) {
$('.childDropdowns').append(createChildDropdown(newVal));
} else if (oldVal > newVal) {
destroyChildDropdown($('.childDropdowns'), newVal);
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form method="post" action="">
<input type="text" class="cat_textbox" name="child" value="0" />
<ul class="button-group button-click-child">
<li><a href="javascript:void(0)"><span class="hide">+</span></a></li>
<li><a href="javascript:void(0)" ><span class="hide">-</span></a></li>
</ul>
<div class="childDropdowns"></div>
<button type="submit">SEND</button>
</form>