As the title says, I'm trying to make jQuery by adding a number of additional fields based on the value selected in the drop-down list. For example, when a page loads, the drop-down menu has a default value of 1, and the default input field is displayed. Now, if the user selects a different value from this drop-down list, the number of fields should be adjusted accordingly by cloning (I suppose this would be a better solution). Here is what I still have:
Dropdown Code ...
<select id="itemCount" name="itemCount">
<option value="1">1 item</option>
<option value="2">2 items</option>
<option value="3">3 items</option>
<option value="4">4 items</option>
<option value="5">5 items</option>
<option value="6">6 items</option>
</select>
... jQuery change listener ...
$(document).ready(function() {
$("#itemCount").change(function(){
var itemCountVal = jQuery(this).val();
$("#item_dup_1").fieldsManage(itemCountVal);
});
});
... ( jQuery, , , , ):
jQuery.fn.fieldsManage = function (number) {
var clone,
objTemplate = source.clone(true),
source = jQuery(this),
maxClones = number - 1,
clones = [];
if (clones.length < maxClones) {
while (clones.length < maxClones) {
clone = objTemplate.clone(true).insertAfter(clones[clones.length - 1] || source);
clones.push(clone);
}
}
if (clones.length > maxClones) {
}
}
, , <fieldset id="item_dup_1"><input><input><input></fieldset>. , .
, , , - , , , . , , , , , . ( , ), .
, - , , , , , !
!