gpt4 book ai didi

javascript - 如何将单选按钮组重置为特定值

转载 作者:行者123 更新时间:2023-11-28 07:26:40 25 4
gpt4 key购买 nike

我有一组单选按钮,我使用 ng-repeat 来显示它们。我默认选择了最后一个单选按钮 (ng-checked="$last"):

Status: <span ng-repeat="button in statusList">
<input type="radio" name="status" ng-model="selectedStatus" value="{{button.value}}" ng-checked="$last"/>{{button.name}}</span>

我的“statusList”如下:

$scope.statusList = [
{"name": "Active", "value": "active"},
{"name": "Inactive", "value": "inactive"},
{"name": "Either", "value": "either"}
];

它按原样工作,但是,我还希望有一个重置/清除表单按钮,无论用户选择什么,它都会再次选择最后一个单选按钮,并且我无法让 Angular 做到这一点。到目前为止,我看到的代码示例只是取消选择所有单选按钮,而不是返回到默认的 $last 按钮。我的清除表单方法如下(去掉表单中的其他元素):

$scope.clearSearchData = function() {
$scope.selectedStatus = "either";
$scope.MyForm.$setPristine();
};

第一次按下“清除”按钮时,上面的代码可以将所选按钮重置为最后一个按钮(如果我硬编码不同的值,则重置为另一个单选按钮),但是随后的时间没有效果,保留用户的任何按钮最后按下仍然有效。每次我点击清除按钮时,其他表单元素都会不断重置,我只是无法让单选按钮正常工作。 (如果有必要,请使用 Chrome。)

最佳答案

您应该使用ng-valueselectedStatusng-model 一起使用,因为单选按钮上的 ng-checked 仅在第一次作为默认值工作,

关于javascript - 如何将单选按钮组重置为特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29580890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com