[英]Select option box based on value of an input box in appended selectbox
I have a selectbox that shows number of childeren. 我有一个选择框,用于显示childeren的数量。 when the user change selectbox it appends selectboxes for showing age of each child.
当用户更改选择框时,它将附加选择框以显示每个孩子的年龄。 moreover I have an input box with type readonly .
此外,我有一个类型为readonly的输入框。 how can I select each child age based on value of input type with classname age ?
如何根据具有类别名称age的输入类型的值选择每个孩子的年龄?
i get values like this : 1,2
我得到像这样的值:1,2
the first value is for the first selectbox and the second value is for second selectbox . 第一个值用于第一个选择框,第二个值用于第二个选择框。 and if user change select box froM the first i want to clear the selected option.
如果用户从第一个更改选择框,则我想清除选择的选项。
is there a way to do this ? 有没有办法做到这一点 ? here is my snippet :
这是我的片段:
$(document).ready(function(){ $('.countRoom').find('.childnum').val($('.childcount').val()); $('.countRoom').find('.childnum').change() }) function childAge(a){ $(a).each(function(){ age=$(a).val(); $(a).closest(".countRoom").find(".selectAge").empty(); for(var j=1;j<=age;j++){ $(a).closest(".countRoom").css("width","100%").find(".selectAge").append('<div class="age"><label>Age of Child' + j + '</label><select name="childage" class="childage form-control"><option value="1">Up to 1 years</option><option value="2">1 to 2 years</option></select></div>'); } }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="countRoom"> children <select onChange="childAge(this)" class="childnum"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> </select> <input type="text" class="childcount" value="2" readonly style="display:none;"> <input type="text" class="age" value="1,2" readonly style="background:green;"> <div class="selectAge"></div> </div>
First you have to get the value of input.age
and then split it. 首先,您必须获取
input.age
的值,然后将其拆分。 After that you can use that value to set selected
attribute in your loop
之后,您可以使用该值在
loop
设置selected
属性
$(document).ready(function() { $('.countRoom').find('.childnum').val($('.childcount').val()); $('.countRoom').find('.childnum').change() }) function childAge(a) { var ageVals = $('input.age').val().split(',') $('input.age').val(',') $(a).each(function() { age = $(a).val(); $(a).closest(".countRoom").find(".selectAge").empty(); for (var j = 1; j <= age; j++) { $(a).closest(".countRoom").css("width", "100%").find(".selectAge").append('<div class="age"><label>Age of Child' + j + '</label><select name="childage" class="childage form-control"><option value="1" ' + (ageVals[j - 1] == 1 ? 'selected' : '') + '>Up to 1 years</option><option value="2" ' + (ageVals[j - 1] == 2 ? 'selected' : '') + '>1 to 2 years</option></select></div>'); } }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="countRoom"> children <select onChange="childAge(this)" class="childnum"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> </select> <input type="text" class="childcount" value="2" readonly style="display:none;"> <input type="text" class="age" value="1,2" readonly style="background:green;"> <div class="selectAge"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.