table单元格 单击 变成下拉框,失去焦点后,变成文本形式

效果图: 点击前
这里写图片描述
点击后:
这里写图片描述
失去焦点后:
这里写图片描述

代码:
先是给td加了一个showing 事件 ,目的是为了实现点击文本时,塞入一个select下拉框

<td class="center" onclick="showing()" id="showing">
    <c:if test="${employes.sex eq '0'}">女</c:if> 
    <c:iftest="${employes.sex eq '1'}">男</c:if>
</td>

然后,我在table外面,写了一个div 域:

<div id="div1" style="display:none">
    <select id="sex" onblur="removethis(this)" style="width:40px;">
        <option value="0"></option>
        <option value="1"></option>
    </select>
</div>

js代码:

function showing(){
              if(event.srcElement.tagName!="TD") return;//如果点击了未做改变,还原文本
              var t = event.srcElement.innerHTML;//得到改变前的值
              event.srcElement.innerHTML = div1.innerHTML;//点击后,引入div域
              event.srcElement.children[0].value = t;//得到选择值
              event.srcElement.children[0].style.width = "100%";//设置下拉框宽度
              var id = $("#showing").parent().parent().find("input[name='ids']").attr('id');
        } 

        function removethis(obj){
            var value = $("#sex option:selected").text();//得到选择的值,这个地方我传的是text,因为后台我做了文本判断,建议传value
            $(obj).parent().text(value);//把选择的值变成文本放到td中
            $(obj).remove();//移除select
            showing();
            var id = $("#showing").parent().parent().find("input[name='ids']").attr('id');
            var url = "<%=basePath%>"+"employee/updateDutySexByUserId.do";
             $.ajax({    
                 type: 'POST',
                 url:url,   
                 data: { "userId": id, "Sex": value},  
                 dataType: "json",
                 async: true,
               }); 
可以通过在表格中的单元格内添加下拉框来实现该功能,具体步骤如下: 1. 在表格中指定单元格内添加一个 `div` 元素,该元素用于容纳下拉框。 ```html <table class="layui-table"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>18</td> <td> <div class="dropdown"> <select> <option value="男">男</option> <option value="女">女</option> </select> </div> </td> </tr> <!-- 其他行省略 --> </tbody> </table> ``` 2. 使用 `layui.form.render()` 方法对表单进行渲染,以便 `select` 元素能够正确显示。 ```html <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script> <script> layui.use(['form'], function () { var form = layui.form; form.render(); }); </script> ``` 3. 使用 jQuery 监听单元格的点击事件,并在点击事件中显示或隐藏下拉框。 ```html <script> $(function () { $('.dropdown').click(function (event) { event.stopPropagation(); // 防止单击事件冒泡到表格行 $('.dropdown').not(this).removeClass('open'); $(this).toggleClass('open'); }); $('html').click(function () { $('.dropdown').removeClass('open'); }); }); </script> ``` 4. 样式表中添加下拉框的样式。 ```html <style> .dropdown { position: relative; display: inline-block; } .dropdown select { width: 100%; padding: 6px 12px; border: 1px solid #d2d6de; border-radius: 2px; background-color: #fff; background-image: none; -webkit-appearance: none; -moz-appearance: none; appearance: none; cursor: pointer; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 1; } .dropdown.open select { z-index: 2; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值