我从一个选择框中获取金额并复制 div 的次数。但是我遇到了一个问题。我需要新的 div(包含选择框)来更改它们的名称。我需要它将递增数字添加到选择框名称的末尾。这是我的 JSFIDDLE:http: //jsfiddle.net/liveandream/DhCLK/
查询:
//NUMERATE SELECT
for (var i = 2; i < 5; i++) { // add counts in a for loop
$('select[name="rooms"]').append('<option value=' + i + '>' + i + '</option>');
}
//#
//DUPLICATE PLUGIN
$.fn.duplicate = function(count, cloneEvents) {
var tmp = [];
for (var i = 0; i < count; i++) {
$.merge(tmp, this.clone(cloneEvents).get());
}
return this.pushStack(tmp);
};
//SELECT CHANGE FUNCTION (on change get value and clone)
$('select[name="rooms"]').change(function(){ // on change...
var numOfClones = $(this).val() -1; // get value...
$('#holder').html(''); // empty holder if there are some old clones
$('.repeat').duplicate(numOfClones).addClass('new').appendTo('#holder')
.find("select").each(function() {
this.name = this.name.replace(/\d+/, 2);
});
// duplicate; fill holder with new clones; the class 'new' is just for styling
});
e.preventDefault();
HTML:
<select name="rooms">
<option>1</option>
</select>
<div class="repeat">
<h4 style="font-size: 11px;">Adults (17+)</h4>
<select name="adults">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<h4 style="font-size: 11px;">Children</h4>
<select name="children" id="children">
<option selected="selected">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<h4 style="font-size: 11px;">Child AGES</h4>
<select name="ages" id="ages" style="display: none;">
<option selected="selected">-?-</option>
<option>>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
</select>
</div>
<div id="holder"></div>
我希望有人可以帮助我...提前谢谢你!</p>