我想将复选框中的值存储到字符串中
<label style="color:black; text-decoration:none; font-size:15px" href="#">
<input type="checkbox" ng-model="diet.v1" ng-true-value="'&diet=high-fiber'" ng-false-value="''" />High-Fiber</label>
如何从 ng-true-value
(选中时)获取值到字符串以便我可以使用它?
当我尝试像这样的东西时 console.log(diet.v1);在 js 中它不会工作
最佳答案
请使用 ng-true-value、ng-false-value 检查以下工作示例代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-checkbox-input-directive-production</title>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
</head>
<body ng-app="checkboxExample">
<script>
angular.module('checkboxExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.diet = {
v1 : ''
};
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
<label>Value:
<input type="checkbox" ng-model="diet.v1"
ng-true-value="'&diet=high-fiber'" ng-false-value="''">
</label><br/>
<tt>value = {{diet.v1}}</tt><br/>
</form>
</body>
</html>
关于javascript - 如果选中,将复选框中的值存储到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48178293/