我正在练习使用Angular处理表单,并且我喜欢使用引导程序使事情看起来更漂亮。由于某种原因,引导程序样式将应用于表单的其余部分,但是只有单选按钮显示为默认的HTML样式。
我已经尝试弄乱了放置类名的位置,更改了输入的包装方式,但是没有任何效果。我应该提一下,我正在在线上学习教程,并且我的代码与讲师的完全匹配。然而,他以漂亮的蓝色和白色引导出现,而我的HTML则保持灰色和黑色。我想解决此问题,使其显示为蓝色和白色,请不要回答告诉我它“应该”为灰色和黑色。
HTML
<form (ngSubmit)="onSubmit()" #f="ngForm">
<div
id="user-data"
ngModelGroup="userData"
#userData="ngModelGroup">
<div class="form-group">
<label for="username">Username</label>
<input
type="text"
id="username"
class="form-control"
ngModel
name="username"
required>
</div>
<div class="form-group">
<label for="email">Mail</label>
<input
type="email"
id="email"
class="form-control"
ngModel
name="email"
required
#email = "ngModel"
>
</div>
</div>
<div class="radio" *ngFor="let gender of genders">
<label>
<input
class="radio"
type="radio"
name="gender"
ngModel
[value]="gender">
{{gender}}
</label>
</div>
<button
class="btn btn-primary"
type="submit"
[disabled]="!f.valid"
>Submit</button>
</form>
TypeScript
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@ViewChild('f') signupForm: NgForm;
defaultValue = 'teacher';
answer = '';
genders = ['male', 'female'];
}
答案 0 :(得分:1)
您需要创建一个自定义单选按钮
示例从上面的链接复制
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
<!DOCTYPE html>
<html>
<body>
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
</body>
</html>