浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
ZC跨境爬虫7 小时前
跟着 MDN 学CSS day_16:(深入掌握背景与边框的艺术)
前端·css·ui·html·tensorflow
道里9 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验
前端·人工智能
Royzst10 小时前
xml知识点
java·服务器·前端
IT_陈寒10 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
kyriewen11 小时前
推行AI写代码一年后,Code Review变成了新的加班理由
前端·ai编程·cursor
前端环境观察室11 小时前
给 Agent Browser Workflow 加一层可观测性:Trace、Snapshot 和 Review Queue
前端
柒瑞11 小时前
Superpowers结合Claude code浅实战
前端
Nian.Baikal12 小时前
从零搭建离线地图服务:Nginx + Cesium/Leaflet 实战指南
运维·前端·nginx
zithern_juejin12 小时前
new 运算符
javascript
前端毕业班12 小时前
uniapp web 灵活控制 style scoped
前端·javascript·vue.js