我在Yii项目中的HTML选择有问题。我有隐藏的选择选项,在我选择国家后,区域选择选项显示为当前国家。选择只显示1行,当我在选择中向下滚动时,我可以看到国家的正确选项。
有人知道问题出在哪里吗?谢谢
下面是我的代码:
于飞:
<div class="form-group">
<label class="control-label" for="region_id"><?= Yii::t('vo-card', 'Region') ?></label>
<select class="form-control" name="region_id" id="select-region">
<option value="" disabled selected><?= Yii::t('vo-card', 'Select') ?></option>
<?php foreach ($region as $item) { ?>
<option value="<?= $item->id ?>" data-country="<?= $item->id_cc_number_country ?>" hidden <?= $item->id == $request['region_id'] ? 'selected' : '' ?>><?= $item->region_name ?></option>
<?php } ?>
</select>
<?php if (isset($errors['region_id'])) { ?>
<div class="text-danger"><?= $errors['region_id'] ?></div>
<?php } ?>
</div>
JavaScript语言:
$(document).on('change', '#select-country', function (event) {
$('#select-region option:not(:disabled)').prop('hidden', true);
$('#select-region option[data-country="' + $(this).val() + '"]').prop('hidden', false);
$('#select-region option').prop('selected', function() {
return this.defaultSelected;
});
event.preventDefault();
});
2条答案
按热度按时间yfjy0ee71#
显然问题就出在这一行:
注意事项:
$(this).val()
-这是所选<option>
的value
属性,但不是data-country
属性的值。您需要通过data-country
-filter选择所有<option>
。下面是您的代码的更正版本:
https://jsfiddle.net/m5ayf9sx/
0aydgbwb2#