篇文章主要的向大家介绍了关于html input标签的单选按钮的使用方法,还有关于HTML input标签的单选默认按钮的做法。接下来我们一起来看看这篇文章吧
<input> 标签用于搜集用户信息。根据不同的type属性值,输入字段拥有很多种形式。输入字段可以是文本字段、复选框、掩码后的文本控件、单选按钮、按钮等等。
> <form action="form_action.asp" method="get">
>
> <input type="radio" name="radio" value="1">单选1
>
> <input type="radio" name="radio" value="2">单选2
>
> <input type="radio" name="radio" value="3">单选3
>
> <input type="radio" name="radio" value="4">单选4
>
> </form>
这个的效果很容易看到,我们还是先看看浏览器中的显示效果吧:
这个效果一眼就能看到,很简单的一个代码
还有种是很多网站上都是经常见到的,比如:单选性别,这个基本上都是用这种单选框去制作的。代码如下:
HTML中的单选按钮实现男女性别选择,不让男女同是都能都能选择,实现方法:在按钮的属性里写一个name属性,并且把name的值设置成相同的
> <input id="man" type="radio" checked="checked" name="1" />男
>
> <input id="woman" type="radio" name="1"/>女
这个就不给图了,比上面那个还简单,就两个单选框,我们经常遇到的这个。
现在来说说HTML单选框按钮怎么默认选中:
首先我们先把第一个实例拿出来继续说,我们只需要在其中加一个属性,如下:
> <form action="form_action.asp" method="get">
>
> <input type="radio" name="radio" value="1">单选1
>
> <input type="radio" name="radio" value="2" checked>单选2
>
> <input type="radio" name="radio" value="3">单选3
>
> <input type="radio" name="radio" value="4">单选4
>
> </form>
这上面我没做任何的点击,自己出现在那上面的,刷新过后还能看到在单选2上面。
我们就可以看到,这样就把单选框给默认选中了,大家可以自己试试,多敲敲代码。
好了,以上就是这篇关于html input标签做单选按钮的文章了,有问题的可以在下方提问。
以上就是html单选按钮默认选中怎么做?input标签的单选按钮用法实例的详细内容,更多请关注我!!!
我自己是一名从事了多年开发的web前端老程序员,目前辞职在做自己的web前端私人定制课程,今年我花了一个月整理了一份最适合2020年学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴,想要获取的可以关注我的头条号并在后台私信我:前端,即可免费获取。
制表单中的单选按钮
<html>
<head>
<title>单选项</title>
<style>
<!--
form{
padding:0px; margin:0px;
font:14px Arial;
}
p{
padding:2px; margin:0px;
}
-->
</style>
<script language="javascript">
function getChoice(){
var oForm = document.forms["myForm1"];
var aChoices = oForm.camera;
for(i=0;i<aChoices.length;i++) //遍历整个单选项表
if(aChoices[i].checked) //如果发现了被选中项则退出
break;
alert("您使用的相机品牌是:"+aChoices[i].value);
}
function setChoice(iNum){
var oForm = document.forms["myForm1"]; //获取表单的节点
oForm.camera[iNum].checked = true; //设置某选项被选中
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="addInfo.php">
您使用的相机品牌:
<p>
<input type="radio" name="camera" id="canon" value="Canon">
<label for="canon">Canon</label>
</p>
<p>
<input type="radio" name="camera" id="nikon" value="Nikon">
<label for="nikon">Nikon</label>
</p>
<p>
<input type="radio" name="camera" id="sony" value="Sony" checked>
<label for="sony">Sony</label>
</p>
<p>
<input type="radio" name="camera" id="olympus" value="Olympus">
<label for="olympus">Olympus</label>
</p>
<p>
<input type="radio" name="camera" id="samsung" value="Samsung">
<label for="samsung">Samsung</label>
</p>
<p>
<input type="radio" name="camera" id="pentax" value="Pentax">
<label for="pentax">Pentax</label>
</p>
<p>
<input type="radio" name="camera" id="others" value="其它">
<label for="others">others</label>
</p>
<p><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn"></p>
<p><input type="button" value="检测选中对象" onclick="getChoice();">
<input type="button" value="设置为Canon" onclick="setChoice(0);"></p>
</form>
</body>
</html>
设置复选框(全选、全不选、反选):
*请认真填写需求信息,我们会在24小时内与您取得联系。