浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
simon_luv_pho5 小时前
一行代码把网页变成 AI Agent?
前端
兆子龙5 小时前
模块联邦(Module Federation)详解:从概念到手把手 Demo
前端·架构
ZFSS5 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
没想好d5 小时前
通用管理后台组件库-8-表格组件
前端
前端Hardy5 小时前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy5 小时前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
布列瑟农的星空6 小时前
rsbuild mock 插件开发指南
前端
用泥种荷花6 小时前
【LangChain.js学习】 文档加载(Loader)与文本分割全解析
前端
cxxcode7 小时前
Vite 热更新(HMR)原理详解
前端
HelloReader7 小时前
Tauri 架构从“WebView + Rust”到完整工具链与生态
前端