我的情况是:
我的加分是
<div class="multi-field">
<select class="text-one" name="destination[]">
<option selected value="base">Please Select</option>
<option value="colombo">Colombo</option>
<option value="kandy">Kandy</option>
<option value="anuradhapura">Anuradhapura</option>
</select>
<br />
<select class="text-two" name="attraction_or_activity[]">
<option value="attraction">Attraction</option>
<option value="activity">Activity</option>
</select>
<select id="populated_attr_or_activity" name="attraction_or_activity_selected[]">
<!-- here I have to populate the ARRAYS as option -->
</select>
</div>
我的jquery是
<script>
$(document).ready(function() {
$(".text-two").change(function() {
/* saving selected values in variables */
var selected_destination = $('.text-one :selected').val();
var selected_attraction_or_activity = $('.text-two :selected').val();
colombo_attractions = new Array("Ganga Ramaya","National Art Gallery","Galle Face Green","Arcade Indepentent Square");
colombo_activities = new Array("City Walk 2016","Traditional Dance Competition 2016","Local Spicy food");
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'attraction') {
colombo_attractions.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'activity') {
colombo_activities.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
});
});
</script>
我几乎得到了它。 当用户选择第二个(吸引力/活动)下拉列表时,根据选择加载第三个。 它加载'活动',可以在'科伦坡'中找到
问题: 当用户切换回其他选择的景点或活动下拉列表时,第3个下拉列表geeting添加了选项。 但我想删除最后加载的选项,并根据新的景点/活动下拉列表选择添加选项。
以下是演示:http://codepen.io/foolishcoder7721/pen/oboKML
我如何解决这个问题并按照我上面的解释来解决?
答案 0 :(得分:1)
排在最前面的第三个清单:
$(document).ready(function() {
$(".text-two").change(function() {
$('#populated_attr_or_activity').html('');
/* saving selected values in variables */
var selected_destination = $('.text-one :selected').val();
var selected_attraction_or_activity = $('.text-two :selected').val();
colombo_attractions = new Array("Ganga Ramaya","National Art Gallery","Galle Face Green","Arcade Indepentent Square");
colombo_activities = new Array("City Walk 2016","Traditional Dance Competition 2016","Local Spicy food");
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'attraction') {
colombo_attractions.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'activity') {
colombo_activities.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
});
});