简体   繁体   English

当我 select 类别使用 Javascript(国家/州/州)时更改选项

[英]Change options when I select a category using Javascript (Country/State)

Im trying to hide some options when I select a different country.当我在另一个国家/地区拨打 select 时,我试图隐藏一些选项。 These options are the states of this country.这些选项是这个国家的州。 But, when I select the country, it doesnt showing nothing in the subcategory.但是,当我 select 国家时,它在子类别中没有显示任何内容。

<script>
    $('#profile-state').find('option').hide();
    $('#profile-country').change(function() {
        var $cat = $(this).find('option:selected');
        var $subCat = $('#profile-state').find('.' + $cat.attr('value'));
        $('#profile-state').find('option').not("'"+ '.' + $cat.attr('value') + "'").hide();
        $subCat.show();
        $subCat.find('option').first().attr('selected', 'selected');
    });
</script>
<div class="form-group field-profile-country required">
<label class="control-label" for="profile-country">País</label>
<select id="profile-country1" class="form-control" name="Profile[country]" aria-required="true">
<option value="">Selecione:</option>
<option value="United States">Estados Unidos</option>
<option value="Brazil">Brasil</option>
</select>
</div>
<div class="form-group field-profile-state required">
<label class="control-label" for="profile-state">Estado</label>
<select id="profile-statew" class="form-control" name="Profile[state]" aria-required="true">
<option value="">Selecione:</option>
<option value="Dont Say">Dont Say</option>
<option value="Unidade States 1">New Work</option>
<option value="Unidade States 2">Washington</option>
<option value="Brazil 1">Rio de Janeiro</option>
<option value="Brazil 2">São Paulo</option>
</select>
</div>

So, I try make differents values using numbers, coz I cant use the same values in my application.所以,我尝试使用数字制作不同的值,因为我不能在我的应用程序中使用相同的值。 Someone can help me?有人可以帮助我吗?

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(^).您的代码中有一些拼写错误,例如子类别下拉列表中美国的拼写错误等。我已将您的脚本更改为 select 只有那些以所选国家/地区名称开头的子类别选项使用(^)。

 $(document).ready(function() { $('#profile-state').find('option').hide(); $('#profile-country').change(function() { var $cat = $(this).find('option:selected'); console.log($cat); var $subCat = $("#profile-state").find('option[value^="'+$cat.attr('value')+' "]'); console.log($subCat); $('#profile-state option:selected').removeAttr('selected'); $('#profile-state').find('option').not('option[value^="'+$cat.attr('value')+' "]').hide(); $subCat.show(); $subCat.find('option').first().attr('selected', 'selected'); }); });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <div class="form-group field-profile-country required"> <label class="control-label" for="profile-country">País</label> <select id="profile-country" class="form-control" name="Profile[country]" aria-required="true"> <option value="">Selecione:</option> <option value="United States">Estados Unidos</option> <option value="Brazil">Brasil</option> </select> </div> <div class="form-group field-profile-state required"> <label class="control-label" for="profile-state">Estado</label> <select id="profile-state" class="form-control" name="Profile[state]" aria-required="true"> <option value="">Selecione:</option> <option value="Dont Say">Dont Say</option> <option value="United States 1">New Work</option> <option value="United States 2">Washington</option> <option value="Brazil 1">Rio de Janeiro</option> <option value="Brazil 2">São Paulo</option> </select> </div>

T.Shah's answer works as required. T.Shah 的回答按要求工作。 But you could do it with less effort like this too:但是你也可以像这样用更少的努力来做到这一点:

 $(document).ready(function() { const $cntr=$('#profile-country').change(function() { $("#profile-state option").each(function(){$(this).toggle(this.value.slice(0,5)==$cntr.val().slice(0,5)) }); }); });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <div class="form-group field-profile-country required"> <label class="control-label" for="profile-country">País</label> <select id="profile-country" class="form-control" name="Profile[country]" aria-required="true"> <option value="">Selecione:</option> <option value="United States">Estados Unidos</option> <option value="Brazil">Brasil</option> </select> </div> <div class="form-group field-profile-state required"> <label class="control-label" for="profile-state">Estado</label> <select id="profile-state" class="form-control" name="Profile[state]" aria-required="true"> <option value="">Selecione:</option> <option value="Dont Say">Dont Say</option> <option value="United States 1">New Work</option> <option value="United States 2">Washington</option> <option value="Brazil 1">Rio de Janeiro</option> <option value="Brazil 2">São Paulo</option> </select> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM