jquery给某个标签如<button>等,添加点击事件,$("#XX").click(function(){ });

本文介绍如何使用jQuery为HTML元素如按钮添加点击事件监听器。通过简单的示例代码展示如何选择特定的DOM元素并绑定点击事件。

jquery给某个标签如<button>等,添加点击事件,$("#XX").click(function(){  });




<script type="text/javascript" src="{%static 'datepicker/js/bootstrap-datepicker.js' %}" ></script> <link rel="stylesheet" type="text/css" href="{% static 'datepicker/css/datepicker.css' %}"> <table class="table table-bordered table-hover text-center" name="device" id="device"> <thead> <tr> <th>设备ID</th> <th>设备名称</th> <th>序列号</th> <th>开始时间</th> <th>截止时间</th> </tr> </thead> <tbody> <!-- 设备行将在这里添加 --> </tbody> </table> <!-- 控制按钮 --> <button id="button_processedorder">添加工单行</button> <button id="button_device">选择设备</button> <button id="button_save">保存工单</button> <script> $(document).ready(function() { $('table#processedorder td').attr('contenteditable', true); $('table#device td').attr('contenteditable', true); <!--解决模态点击关闭,无法关闭的问题--> $('.close').click(function() { $('#device_modal').modal('hide'); }); let newIndex = 1; // 工单行计数器 // 添加工单行 $('#button_processedorder').click(function() { $('#processedorder tbody').append(` <tr data-id="${newIndex}"> <td>${newIndex}</td> <td><input type="text" class="process_order" required></td> <td><input type="text" class="name" required></td> <td><input type="text" class="figure_number" required></td> <td><input type="number" min="1" max="20000" class="count" required></td> <td><input type="date" class="due_date" required></td> <td><input type="number" min="1" max="20000" class="figure_count" required></td> <td><input type="text" class="item_number" required></td> <td><button class="delete-item" data-id="${newIndex}">删除</button></td> </tr> `); newIndex++; }); // 选择设备按钮 $('#button_device').click(function() { $.get('/project/get_devices/', function(response) { let html = ''; {#alert('11111111111111');#} if (response && response.devices) { // 添加空值检查 $.each(response.devices, function(i, device) { html += `<tr> <td>${device.id}</td> <td>${device.name}</td> <td>${device.serial}</td> <td><button class="select-device" data-id="${device.id}">选择</button></td> </tr>`; }); {#alert(html)#} $('#device_table').html(html); $('#device_modal').modal('show'); {#alert('33333333333');#} } else { console.error('设备数据格式错误:', response); } }); // 设备选择事件 $(document).on('click', '.select-device', function() { const deviceId = $(this).data('id'); $.get(`/project/get_device_details/${deviceId}/`, function(device) { $('#device tbody').append(` <tr data-id="${device.id}"> <td>${device.id}</td> <td>${device.name}</td> <td>${device.serial}</td> <td class="datepicker"></td> <td class="editable date-cell"></td> </tr> `); $(this).closest('tr').remove(); <!--必须要有下面这条指令,否则不能编辑--> // 设置倒数第一列和倒数第二列可编辑 $('table#device tr').each(function() { $(this).find('td').eq(-1).attr('contenteditable', true); // 倒数第一列 $(this).find('td').eq(-2).attr('contenteditable', true); // 倒数第二列 $(this).find('td').eq(-1).datepicker(); }); 鼠标点击 可编辑的倒数第一列,弹出来的datepicker,控件不显示 年分,且只有第一行的倒数第二列才会有datepicker弹窗 Django
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值