浅谈监听单选框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("这是个女孩"); 
  } 
 });
});
相关推荐
深盾科技1 小时前
C++ 中 std::error_code 的应用与实践
java·前端·c++
Jagger_2 小时前
我的AI驯服记:从7640px大屏的惨败,到总结出一套高效协作SOP
前端
hy35282 小时前
VUE 踩坑合集
前端·javascript·vue.js
Franciz小测测2 小时前
Gemini 网页端自定义教程:利用 Stylus 强制开启宽屏显示
前端·css·stylus
彭不懂赶紧问2 小时前
鸿蒙NEXT开发浅进阶到精通15:从零搭建Navigation路由框架
前端·笔记·harmonyos·鸿蒙
菩提祖师_2 小时前
基于Flutter的天气查询APP开发
开发语言·javascript·flutter
xkxnq2 小时前
第一阶段:Vue 基础入门(第 2 天)
前端·javascript·vue.js
程序员刘禹锡2 小时前
定位与图标字体知识点全解析!!!(12.31)
前端·css·html·css3
wordbaby2 小时前
公私分明:为什么你不应该共用 SSH Key(附多账号最佳实践指南)
前端·git·ssh
多啦C梦a2 小时前
《双Token机制?》Next.js 双 Token 登录与无感刷新实战教程
前端·全栈·next.js