使用itemz []尝试动态表单字段。
我希望表单字段重复,并从json对象中选择select。
<!-- DYNAMIX ITEMS-->
<div class='container1'>
<tr>
<td>
<select id='itemz' name='itemz[]' class='itemz'> </select>
</td>
<td>
<input class='form-control' type='text' name='count[]' value=''>
<input class='form-control' type='hidden' name='weight[]' value='<?= $items['weight']; ?> '>
</td>
<!-- ADD MORE -->
<td>
<button class='add_form_field'>Add More</button>
</td>
</tr>
</div>
以下是JS的假设: 1)填充下拉列表 但是它只填充了第一个盒子,我尝试在课堂上使用一个但没有运气。
2)复制下拉菜单,但不是在第一个主要div之前将其添加后附加
<script>
// POPULATING THE DROP DOWN
var obj={
Prod:
<?php echo $products; ?>
};
for(var i=0;i<obj.Prod.length;i++)
{
var option=$('<option></option>').text(obj.Prod[i]['item']);
$( ".itemz" ).append(option);
}
// DUPLICATING THE DROP DOWNS
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".container1"); //Fields wrapper
var add_button = $(".add_form_field"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append("<tr><td><select id='itemz' name='itemz[]' class='itemz'></select></td> <td><input class='form-control' type='text' name='count[]' value=''><input class='form-control' type='hidden' name='weight[]' value=''> </td> <td><a href='#' class='remove_field'>Remove</a> </td></tr></div>"); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
这是我的JSON对象:
Prod:
[{"id":"1","item":"Piano","weight":"200"},{"id":"2","item":"Fridge","weight":"50"},{"id":"3","item":"Freezer","weight":"100"},{"id":"4","item":"Sofa","weight":"20"},{"id":"5","item":"Microwave","weight":"10"},{"id":"6","item":"Dining Table","weight":"40"},{"id":"7","item":"Coffee Table","weight":"20"}]
};
答案 0 :(得分:1)
var obj={
Prod:[
{
"id":"1",
"item":"Piano",
"weight":"200"
},{
"id":"2",
"item":"Fridge",
"weight":"50"
},{
"id":"3",
"item":"Freezer",
"weight":"100"
},{
"id":"4",
"item":"Sofa",
"weight":"20"
},{
"id":"5",
"item":"Microwave",
"weight":"10"
},{
"id":"6",
"item":"Dining Table",
"weight":"40"
},{
"id":"7",
"item":"Coffee Table",
"weight":"20"
}
]
};
var max_fields= 10;
var curent_fields=0;
function add_options(_el){
for(var key in obj.Prod){
var text=obj.Prod[key].item;
var id=obj.Prod[key].id;
var weight=obj.Prod[key].weight;
var el =$('<option/>').text(text).val(id).attr('weight',weight);
$(_el).append(el);
}
}
function add_controls(){
if(curent_fields>=max_fields){
alert('max feilds '+max_fields);
return false;
}
$('.container1').append('<tr><td><select name="itemz[]" class="itemz"></select></td><td><input class="form-control" type="text" name="count[]" value=""><input class="form-control" type="hidden" name="weight[]" value=""><a href="javascript:void(0)" class="remove_field">Remove</a></td><td>');
add_options($('.itemz').last());
$('.itemz').last().change(function(){
select_change(this);
});
$('.itemz').last().parent().next().find('.remove_field').click(function(){
$(this).parent().parent().remove();
curent_fields--;
});
curent_fields++;
}
function select_change(_el){
var curent_weight=$(_el).children(':selected').attr('weight');
var curent_id=$(_el).children(':selected').val();
var curent_item=$(_el).children(':selected').text();
//set hidden feid value
$(_el).parent().next().find('[name^="weight"]').val(curent_weight);
//your more code...
console.log([curent_id,curent_weight,curent_item]);
}
function start(){
add_options($('.itemz').last());
$('.itemz').last().change(function(){
select_change(this);
});
}
start();
$('.add_form_field').click(function(){
add_controls();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='container1'>
<tr>
<td>
<select id='itemz' name='itemz[]' class='itemz'> </select>
</td>
<td>
<input class='form-control' type='text' name='count[]' value=''>
<input class='form-control' type='hidden' name='weight[]' value=''>
</td>
<!-- ADD MORE -->
<td>
<button class='add_form_field'>Add More</button>
</td>
</tr>
</div>
&#13;