![](/img/trans.png)
[英]Angular select with ng-repeat loses selected option when options are updated
[英]How to make a <div> show only when a certain <select> dropdown option is selected? The <select> options are populated with the ng-repeat directive
我有一个select
下拉列表,其中填充了angularjs ng-repeat
指令。 我希望div
仅在选择了某个选项时显示。
这是代码:
<select type="text"
class="form-control"
ng-model="vm.request.requestType"
name="requestType" id="requestType"
placeholder=""
required>
<option selected></option>
<option value="test">test</option>
<option ng-repeat="requestType in vm.requestTypes">{{requestType}}</option>
</select>
<script>
$(function() {
$("#requestType").change(function () {
if ($("#test").is(":selected")) {
$("#continueCheckbox").show();
} else {
$("#continueCheckbox").hide();
}
}).trigger('change');
});
</script>
<div id="continueCheckbox">
<input type="checkbox"
name="continueCheckbox"
value="continueCheckbox">
Check this box to continue, and to confirm that you have read the
Documentation
</div>
“测试”选项仅用于测试该功能是否有效。 当前,无论选择什么,该复选框都会显示,并且永远不会消失。
我强烈建议不要将AngularJS和jQuery混合使用。 两者都是DOM操作框架,不能很好地协同工作。 这是使用AngularJS可以完成的工作的一种方式:
<div id="continueCheckbox" ng-if="vm.request.requestType === 'test'">
<input type="checkbox"
name="continueCheckbox"
value="continueCheckbox">
Check this box to continue, and to confirm that you have read the Documentation
</div>
您是否尝试过使用ng-class
指令?
<div id="continueCheckbox" ng-class="selected: vim.request.requestType === 'test'"></div>
然后在.css文件中处理样式
.selected {
display: none;
}
如果您想使用jQuery,则只需检查select的值即可。 您已经设置好了。
var selectType=$(this).val();
if (selectType=="test") // instead of if ($("#test").is(":selected"))
您也可以将香草JS与.value进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.