浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
坚定信念,勇往无前32 分钟前
electron-vite 安装better-sqlite3
javascript·数据库·electron
@菜菜_达40 分钟前
jquery.inputmask插件介绍
前端·javascript·jquery
QuZhengRong40 分钟前
【Luck-Report】缓存
java·前端·后端·vue·excel
jiayong231 小时前
前端面试题库 - 浏览器与网络篇
前端·网络·面试
Csvn1 小时前
小程序开发:微信小程序与 uni-app 实战指南
前端
摸鱼小李上线了1 小时前
vue项目页面添加水印实现方法
前端·javascript·vue.js
砍材农夫1 小时前
物联网 基于netty构建mqtt协议规范(主题通配符订阅)
java·前端·javascript·物联网·netty
彩票管理中心秘书长1 小时前
智能体状态指示:何时思考、何时调用工具、何时出错
前端·后端·程序员
彩票管理中心秘书长1 小时前
React + TypeScript拆解一整套“AI 变现代码流程”
前端·后端·程序员
广州华水科技1 小时前
单北斗GNSS变形监测在基础设施安全中的应用与维护
前端