Im trying to hide some options when I select a different country. These options are the states of this country. But, when I select the country, it doesnt showing nothing in the subcategory.
JavaScript
x
11
11
1
<script>
2
$('#profile-state').find('option').hide();
3
$('#profile-country').change(function() {
4
var $cat = $(this).find('option:selected');
5
var $subCat = $('#profile-state').find('.' + $cat.attr('value'));
6
$('#profile-state').find('option').not("'"+ '.' + $cat.attr('value') + "'").hide();
7
$subCat.show();
8
$subCat.find('option').first().attr('selected', 'selected');
9
});
10
</script>
11
JavaScript
1
9
1
<div class="form-group field-profile-country required">
2
<label class="control-label" for="profile-country">País</label>
3
<select id="profile-country1" class="form-control" name="Profile[country]" aria-required="true">
4
<option value="">Selecione:</option>
5
<option value="United States">Estados Unidos</option>
6
<option value="Brazil">Brasil</option>
7
</select>
8
</div>
9
JavaScript
1
12
12
1
<div class="form-group field-profile-state required">
2
<label class="control-label" for="profile-state">Estado</label>
3
<select id="profile-statew" class="form-control" name="Profile[state]" aria-required="true">
4
<option value="">Selecione:</option>
5
<option value="Dont Say">Dont Say</option>
6
<option value="Unidade States 1">New Work</option>
7
<option value="Unidade States 2">Washington</option>
8
<option value="Brazil 1">Rio de Janeiro</option>
9
<option value="Brazil 2">São Paulo</option>
10
</select>
11
</div>
12
So, I try make differents values using numbers, coz I cant use the same values in my application. Someone can help me?
Advertisement
Answer
There are a few typos in your code, like wrong spelling of united states in the subcategory dropdown etc. I have changed your script to select only those subcategory options that begin with the name of the selected country by using(^).
JavaScript
1
13
13
1
$(document).ready(function() {
2
$('#profile-state').find('option').hide();
3
$('#profile-country').change(function() {
4
var $cat = $(this).find('option:selected');
5
console.log($cat);
6
var $subCat = $("#profile-state").find('option[value^="'+$cat.attr('value')+' "]');
7
console.log($subCat);
8
$('#profile-state option:selected').removeAttr('selected');
9
$('#profile-state').find('option').not('option[value^="'+$cat.attr('value')+' "]').hide();
10
$subCat.show();
11
$subCat.find('option').first().attr('selected', 'selected');
12
});
13
});
JavaScript
1
23
23
1
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
2
<div class="form-group field-profile-country required">
3
<label class="control-label" for="profile-country">País</label>
4
<select id="profile-country" class="form-control" name="Profile[country]" aria-required="true">
5
<option value="">Selecione:</option>
6
<option value="United States">Estados Unidos</option>
7
<option value="Brazil">Brasil</option>
8
</select>
9
</div>
10
11
12
<div class="form-group field-profile-state required">
13
<label class="control-label" for="profile-state">Estado</label>
14
<select id="profile-state" class="form-control" name="Profile[state]" aria-required="true">
15
<option value="">Selecione:</option>
16
<option value="Dont Say">Dont Say</option>
17
<option value="United States 1">New Work</option>
18
<option value="United States 2">Washington</option>
19
<option value="Brazil 1">Rio de Janeiro</option>
20
<option value="Brazil 2">São Paulo</option>
21
</select>
22
</div>
23