我正在通过API调用创建列表。我需要选择该列表中的第一个单选按钮。如果存在列表中只有一个项目的情况,我希望该项目被自动选择,然后定向到下一页。下面的代码仅在视觉上选择单选按钮-除非单击该值,否则不反映该值。
<tbody>
<tr ng-repeat="i in app | filter:searchApp">
<td class="text-center">
<input type="radio" ng-model="$parent.currentApp" id={{i.name}} value="{{i.id}}"
ng-click="getVal(i)" name="radio" ng-checked="$first">
<label class="ui-radio" for={{i.name}}></label>
</td>
<td>{{i.name}}</td>
<td>{{i.description}}</td>
</tr>
</tbody>
$(function() {
var $radios = $('input:radio[name=radio]');
if($radios.is(':checked') === false) {
$radios.filter('[value=i.id]').prop('checked', true);
$rootScope.app= i.id;
console.log($rootScope.app);
}
console.log($radios);
});
我收到此JQuery错误,因为它无法识别value = i.id。请帮助我在页面加载时选择单选按钮,以便选择该值(而不仅仅是视觉检查)。解决方案可以是AngularJS或Jquery。
谢谢你的帮助。
您可以使用纯角度实现所需的结果。这是我创建的有效演示。
简介:创建模型$scope.selectedListItem = null;
从服务器获取数据(在我的例子中$ scope.apiDataList是数据),请执行以下操作以选择第一个单选按钮:
//for multiple elements/options
$scope.apiDataList = [{id:1,name:"male"},{id:2,name:"female"}];
//now when you have got the data, select the first element
$scope.selectedListItem = $scope.apiDataList[0];
现在,您已经选择了第一个单选按钮的实际值,我们现在想在视觉上显示它。在您的代码中使用类似的内容,如下所示:
<input type="radio" ng-model="selectedListItem" id={{item.name}} value="{{item.id}}"
ng-click="selectRadioButton(item)" name="radio" ng-checked="selectedListItem==item">
注意ng-checked="selectedListItem==item"
片段。如果当前单选按钮是选中的,那将标记单选按钮在视觉上被选中。以后需要使用选定列表项/单选按钮的值时,请使用$scope.selectedListItem
。
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句