首页 > 教程 > 前端技术 > 模板制作

用javascript怎么验证单选框为必选

发布:2023-04-11 来源:原创 浏览:207 次 评论:0
核心提示:在前端开发中,使用JS进行判断选框为必选,没选提示用户必须选择实例代码:htmlheadtitleJS判断单选框为必选/titlemeta charset="UTF-8"//headbodyform name="from1"p请选择你的性别/pinput type="radio" id="a" name="sex" value="1"男生 input type="radio&q

在前端开发中,使用JS进行判断选框为必选,没选提示用户必须选择


实例代码:


<html>
    <head>
        <title>JS判断单选框为必选</title>
        <meta charset="UTF-8"/>
    </head>
    <body>
    <form name="from1">
        <p>请选择你的性别</p>
            <input type="radio" id="a" name="sex" value="1">男生 
            <input type="radio" id="b" name="sex" value="2">女生
         <br />
         <button type="button"check();">确定</button>
    </form>
    <script type="text/javascript">
        function check(){
            if(!document.getElementById("a").checked && !document.getElementById("b").checked){
               alert("必须选择性别!")
               return false;
            }
        }
    </script>
    </body>
</html>


 
  • 文本链接:https://mbtxw.com/Course/show.php?itemid=143
  • 文章来源:原创
  • 版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
  • 小黑
  • 文章0
  • 人气342
  • 收藏0
  • 粉丝 0