JSP中radio的使用及js中的点击事件

不经常使用就会忘记,此处做一个笔记,方便以后使用

第一种,也是最简单的一种方式实现,

<%--
  Created by IntelliJ IDEA.
  Date: 2019/7/15
  Time: 9:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试JSP页面中的radio选项</title>
</head>
<%--<script type="text/javascript" src="js/jquery/jquery-1.11.1.min.js"></script>--%>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<body>
<form action="" method="get">
    <div class="">
        <strong>生活组成:</strong>
            <label><input type="radio" name="lifeType" value="1" checked="checked"/>休息</label>
            <label><input type="radio" name="lifeType" value="2" />工作</label>
            <label><input type="radio" name="lifeType" value="3" />学习</label>
    </div>
    获取选择的值: <label id="value" name="value"/>

</form>
<script type="text/javascript">
    $(document).ready(function () {
        $("input:radio[name='lifeType']").click(function(){
            var lifeType = $('input[name="lifeType"]:checked').val();
            if (lifeType == '1' || lifeType == '' || lifeType == null) {
                lifeType = "值为1";
                document.getElementById("value").innerHTML = lifeType;
            } else if (lifeType == '2') {
                lifeType = "值为2";
                document.getElementById("value").innerHTML = lifeType;
            } else if (lifeType == '3') {
                lifeType = "值为3";
                document.getElementById("value").innerHTML = lifeType;
            }
        });
    });
</script>
</body>
</html>

 直接运行上面的程序,即可实现单选按钮的点击功能,运行截图如下所示

以上是最简单的一种方式,还有其他的实现方式,

第二种,以其他方式标签,实例如下所示:

<%--
  Created by IntelliJ IDEA.
  Date: 2019/7/15
  Time: 9:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试JSP页面中的radio选项</title>
</head>
<%--<script type="text/javascript" src="js/jquery/jquery-1.11.1.min.js"></script>--%>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<body>
<form action="" method="get">
    <div class="" id="test">
        <strong>生活组成-test:</strong>
            <label><input type="radio" name="lifeType" value="1" checked="checked"/>休息</label>
            <label><input type="radio" name="lifeType" value="2" />工作</label>
            <label><input type="radio" name="lifeType" value="3" />学习</label>
    </div>
    获取选择的值: <label id="value" name="value"/>

</form>
<script type="text/javascript">
    // $(document).ready(function () {
    //     $("input:radio[name='lifeType']").click(function(){
    //         show();
    //     });
    // });

    $(document).on("click", "#test input", function (event) {
        show();
    });

    function show() {
        var lifeType = $('input[name="lifeType"]:checked').val();
        if (lifeType == '1' || lifeType == '' || lifeType == null) {
            lifeType = "值为1--";
            document.getElementById("value").innerHTML = lifeType;
        } else if (lifeType == '2') {
            lifeType = "值为2--";
            document.getElementById("value").innerHTML = lifeType;
        } else if (lifeType == '3') {
            lifeType = "值为3--";
            document.getElementById("value").innerHTML = lifeType;
        }
    }
</script>
</body>
</html>

运行结果如下所示:

以上是2中实现单选按钮事件的方式,可以根据组件的不同,修改第2中方式中的

$(document).on("click", "#test input", function (event) 中的"#test input"这部分内容即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值