默认有条件选择下拉值
By default Selecting dropdown value conditionally
我有2个选项Month & Year
下拉。根据API调用响应,如果period == Y
,年份默认选择&如果是period == M
,则选择"Month"。我不知道该怎么做。
<div class="col-sm-3 PDL dateY">
<select class="form-control" id="sel1" ng-model="SelectedValue_free" ng-options="details.period for details in validity_dd">
</select>
</div>
$scope.validity_dd = [
{
period: 'Year'
}, {
period: 'Month'
}
]
根据您从api获取信息后的情况,设置
if(period == 'Y')
$scope.SelectedValue_free= 'Year';
else if(period == 'M')
$scope.SelectedValue_free= 'Month';
将ng-options更改为
ng-options="details.period as details.period for details in validity_dd"
或者您可以设置
if(period == 'Y')
$scope.SelectedValue_free= $scope.validity_dd[0];
else if(period == 'M')
$scope.SelectedValue_free= $scope.validity_dd[1];
ng-options="details.period for details in validity_dd"
在app.js中使用以下脚本
$scope.validity_dd = [
{
period: 'Year'
}, {
period: 'Month'
}
]
//this is an array to store all the possible options as key value pairing
$scope.opt_arr={'Y':'Year','M':'Month'};
//Here you can set the value of period with api call response data
$scope.period='M';
//this will search the object from the array which match the data
var result = $.grep($scope.validity_dd, function(e)
{ return e.period == $scope.opt_arr[$scope.period]; });
//this will set the model value so that the required option will be selected
$scope.SelectedValue_free=result[0];
相关文章:
- 通过js在新选项卡中有条件地打开url
- jQuery-有条件地附加HTML
- 有条件更新d3.js力图中节点的最佳方法
- Woocommerce产品选项有条件
- 有条件地在选项标记中应用布尔属性
- ADF:有条件地加载javascript资源
- 如何有条件地更改角度中的orderBy参数
- 有条件地在带有角度的选择中显示选项
- jQuery:有条件地显示基于下拉框选择的元素
- jquery:如果第一个选择器返回为空,有没有一种简明的方法可以有条件地使用第二个选择器
- 根据复选框选择有条件地删除下拉选项
- jQuery UI日期选择器条件混乱.有更干净的方法吗
- 确定单击了哪个元素,并有条件地选择要调用的方法
- 在RxJS中有条件地选择observable
- 如何有条件地测试从下拉列表中选择的选项/值
- 如何禁用数据库中有条件值的选择
- 默认有条件选择下拉值
- 有条件地显示/隐藏文本区域与选择.Javascript中的onchange
- 如何有条件地阻止jQueryUi日期选择对话框打开
- 有条件地显示和隐藏从数据库生成的选择输入选项