浅谈监听单选框radio改变事件(和layui中单选按钮改变事件)_javascript技巧

若是只引用jquery的话,监听单选按钮改变事件如下:

javascript 复制代码
<tr>
                    <td align="left" class="bigColor">房屋类型</td>
                    <td colspan="5">
                        <input type="radio" name="houseType" id="houseType1" value="1">1 土屋
                        <input type="radio" name="houseType" id="houseType2" value="2">2 茅屋
                        <input type="radio" name="houseType" id="houseType3" value="3">3 木屋
                        <input type="radio" name="houseType" id="houseType4" value="4">4 砖瓦平房
                        <input type="radio" name="houseType" id="houseType5" value="5">5 砖瓦楼房
                        <input type="radio" name="houseType" id="houseType6" value="6">6 其他
                        <input type="text" id="houseTypeOther" name="houseTypeOther" maxlength="200"/>
                    </td>
                </tr>
javascript 复制代码
/*
    * 字段监听
    * */
    $(document).ready(function() {
        $('input[type=radio][name=houseType]').change(function() {
            if (this.value == '6') {
                $("#houseTypeOther").removeProp("readonly");
            } else {
                $("#houseTypeOther").attr("value", "");
                $("#houseTypeOther").prop("readonly", true);
            }
        });
    });

注意:如果是引用了layui的样式和方法,则上面的监听事件失效,被layui接管了,所以应该如下写法:

javascript 复制代码
layui.use(['form', 'element', 'laydate'], function(){
 var jQuery = layui.jquery
 ,layer = layui.layer
 ,form = layui.form()
 ,element = layui.element();

 form.on("radio(sex)", function (data) {
  var sex = data.value;
  if (this.value == '1') { 
   alert("这是个男孩"); 
  } else if (this.value == '2') { 
   alert("这是个女孩"); 
  } 
 });
});
相关推荐
烬羽21 小时前
React Router v7 完全指南:一个 Demo 吃透前端路由
javascript·react.js·前端框架
烬羽21 小时前
从"整个页面刷新"到"丝滑切换":手写一个 HashRouter 彻底搞懂前端路由
javascript·前端框架·全栈
用户9385156350721 小时前
从零理解 React Router v6:每一个 API 都是怎么工作的
前端·javascript·全栈
用户059540174461 天前
Redis缓存回滚踩坑实录:一个配置失误,让我在凌晨3点丢了3000条数据
前端·css
kyriewen1 天前
别再这样写条件渲染了——你的React组件里藏着这5种定时炸弹
前端·javascript·react.js
IT_陈寒1 天前
Redis缓存击穿把我坑惨了,原来这样设过期时间才靠谱
前端·人工智能·后端
用户938515635071 天前
从"坐电梯"到"前端路由"——深入理解 Hash 路由原理
前端·typescript·全栈
梦想CAD控件1 天前
网页端CAD的图形选择、编辑与夹点操作教程
前端·javascript·node.js
妙码生花1 天前
从 PHP 到 AI + Golang,程序员自救转型手记(五十二):管理员权限检查中间件,AI 随意放行预闯大祸
前端·后端·go
月月大王的3D日记1 天前
Three.js 入门系列(8):六种光源全解析 —— 关灯了,开光!
前端·javascript