在选择时为单选按钮分配唯一id
assign unique id to radio button on selection
我试图在选择上分配唯一的id给单选按钮。我不确定这是正确的方式。我试过这样做:
<!DOCTYPE html>
<html ng-app="radioExample">
<head>
<script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body>
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.startDate = {
id: new Date()
};
$scope.specialValue = {
"value": new Date().getMilliseconds()
};
$scope.uniqueId = new Date().getMilliseconds();
}]);
</script>
<form ng-controller="ExampleController" name="myForm">
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::uniqueId}}" />
Jan
</label>
<br />
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::uniqueId}}" />
Feb
</label>
<br />
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::uniqueId}}" />
Mar
</label>
<br />
<tt>ID = {{uniqueId | json}}</tt>
<br />
</form>
Note that `ng-value="specialValue"` sets radio item's value to be the value of `$scope.specialValue`.
</body>
</html>
即使添加了以毫秒为单位的时间戳,也会为所有单选按钮打印相同的值
这是添加相同的值,因为它是相同的值,你只使用一个变量,在控制器上分配一次,然后使用相同的变量三次。
无论如何,为什么你需要为每个输入分配一个唯一的id。
你可以解决这个问题,在你的控制器上声明一个返回唯一id的函数,并从你的视图调用它。
你可以像这样在你的作用域中创建一个函数:
JSFiddle演示HTML:<!DOCTYPE html>
<html ng-app="radioExample">
<head>
<script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body>
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.startDate = {
id: new Date()
};
$scope.specialValue = {
"value": new Date().getMilliseconds()
};
$scope.getUId = function() {
var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charSetSize = charSet.length;
var id = '';
for (var i = 1; i <= 4; i++) {
var randPos = Math.floor(Math.random() * charSetSize);
id += charSet[randPos];
}
return id;
}
}]);
</script>
<form ng-controller="ExampleController" name="myForm">
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::getUId()}}" /> Jan
</label>
<br />
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::getUId()}}" /> Feb
</label>
<br />
<label>
<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{::getUId()}}" /> Mar
</label>
<br />
</form>
</body>
</html>
edit:您可以更改charSet
以适合您的唯一id中可接受字符的域。
解决方案:
html:<input type="radio" name="rdboption" ng-value="specialValue" ng-model="startDate.id" id="{{uniqueId()}}" />
js:
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.startDate = {
id: new Date()
};
$scope.specialValue = {
"value": new Date().getMilliseconds()
};
$scope.uniqueId = function() {
return new Date().getMilliseconds();
};
}]);
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 在循环中分配json值时,值被覆盖
- 如何为动态创建的元素分配唯一的id-Javascript
- 使用Javascript为Pyramid的每个块分配唯一的id
- 为动态创建的元素分配唯一名称
- 具有当前日期的元素的日期,并分配唯一ID
- 分配唯一的id和名称
- 使用全局变量为动态创建的元素分配唯一id和唯一名称
- 如何为一对克隆对象分配唯一的id和href
- 通过find()和each()迭代分配唯一的ID
- 如何分配或附加一个唯一的ID在javascript与行整数从html
- 为angular指令实例分配自定义的唯一id
- 分配,在主索引页上为脚本使用唯一的Wordpress帖子id
- 创建分配唯一项的算法
- 我想为每个用户分配一个唯一的URL-PHP
- 在Jquery中为类的所有元素分配唯一ID
- 如何为每个复选框分配一个唯一的id
- 如何使用jquery/javascript为html中的ID列生成和分配唯一编号
- 如何为以下方案的每个选择框分配唯一 ID
- 在选择时为单选按钮分配唯一id