浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
Eiceblue6 分钟前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#
悟空瞎说6 分钟前
从isMounted到跨页面状态:高级前端如何优雅解决订单场景的“幽灵陷阱”(附React/Vue完整代码)
前端·javascript
C_fashionCat8 分钟前
【2026面试题】前端实际场景去考察原理
前端·vue.js·面试
落魄江湖行9 分钟前
进阶篇三 Nuxt4 Nitro 引擎:Nuxt 的服务端核心
前端·vue.js·typescript·nuxt4
sheeta199811 分钟前
TypeScript references 配置与 emit 要求详解
javascript·ubuntu·typescript
一壶纱12 分钟前
Element Plus 主题构建方案
前端·vue.js
程序员马晓博12 分钟前
我的大脑不下班:一个前端工程师的工作反刍自救指南
前端
吴声子夜歌13 分钟前
Vue3——表单元素绑定
前端·vue·es6
神の愛15 分钟前
js的深拷贝和浅拷贝?啥情况讲解下??底层堆栈空间??object.prototype.toString.call(),还有bind,的具体使用?
前端·javascript·原型模式
浩星15 分钟前
「React + Cesium 最佳实践」完整工程化方案
前端·vue.js·react.js