浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
钛态11 小时前
前端安全防线:CSRF 攻击链路与双重 Token 校验的工程实现
前端·vue·react·web
张拭心12 小时前
技术管理笔记:让我“燃尽”的一天
前端
不好听61313 小时前
BFF 架构实战:从前端直调 API 到加入中间层
前端·架构
不好听61313 小时前
前端跨域完全指南:从为什么报错到三种解决方案
前端
三84413 小时前
路由策略/控制 配置双点双向路由重发布
服务器·前端·javascript
Qimooidea14 小时前
祁木 CAD Translator 工程图纸出海实战指南
服务器·前端·安全
小林ixn14 小时前
从BFF到SSE:我在Vue项目里藏了个“AI翻译官”
前端·vue.js·vite
元气少女小圆丶15 小时前
unity发布web嵌入到前端页面的接受参数
前端·unity·webgl
工业HMI实战笔记15 小时前
【拯救HMI】:低碳制造:自动化技术如何助力企业节能降耗
运维·前端·自动化·交互·制造
济*沧*海15 小时前
Layui的学习路径
layui