我正在使用 Bootstrap 创建登录页面,这是我的代码
<form class="form-signin" action="firstLogin.action" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" name="username" placeholder="User Name"
required="required" autofocus>
<br>
<input type="password" class="form-control" name="password" required="required"
placeholder="Password">
<br>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn btn-primary" style="width: 150px">Admin</button>
<button type="button" class="btn btn-primary" style="width: 150px">User</button>
</div>
<div><br></div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
我想将单选按钮字段设为必填字段。知道怎么做吗?
最佳答案
为了使输入成为必需,元素必须是输入元素。将您的按钮更改为输入,给它们命名,添加必需,并将类型更改为“ radio ”,并且必需应该可以工作(see this fiddle):
<div>
<input type="radio" name="radio-choice" required>Admin</input>
<input type="radio" name="radio-choice" required>User</input>
</div>
不幸的是,这意味着它们看起来不再像很酷的按钮了。您可以将单选按钮设置为您想要的样式,并且 required 仍然会阻止表单提交,但您不会看到选择选项的弹出警告( see this fiddle )。
所以,对于你想要的,看起来你必须做一些基本的编程。 Here's a fiddle使用原始按钮进行基本检查。
关于twitter-bootstrap - 使用 Bootstrap 将单选按钮设为必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881658/