django中循环生成的多个btn,只有第一个btn会弹出模态框

django中循环生成的多个btn,只有第一个btn会弹出模态框

需求:为每个button按钮都绑定同一点击事件,点击每个btn都可弹出模态框

原因

  1. 问题代码
html 复制代码
<button id='btnDel' type="button" class="btn btn-primary btn-lg" > [删除] </button>

<script type="text/javascript">
	$(function (){
		bindBtnDelEvent();
	})
	function bindBtnDelEvent() {
		$("#btnDel").click(function () {
			// 点击id为btnDown的按钮,出现对应的模态框
			$('#myModal').modal('show');
		});
	}
</script>
  1. 在使用$('#btnDel')的方法寻找id,只能为第一个btn绑定点击事件,因为jQuery中通过$匹配标签id的方法默认仅寻找第一个。

解决方法

  1. 解决方法:为btn添加自定义class,通过寻找class的方式为每个button绑定同一个点击事件。如这里的"btnDel":
  2. 需要修改的代码
html 复制代码
<button type="button" class="btnDel btn btn-primary btn-lg" > [删除] </button>

$(".btnDel").click(function () { // js需修改的部分

以上就是全部内容

相关推荐
金銀銅鐵几秒前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf18 分钟前
Python 模块与包的导入导出
前端·后端·python
ice81303318142 分钟前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
夜微凉442 分钟前
三、Spring
java·后端·spring
copyer_xyf43 分钟前
Python venv 虚拟环境
前端·后端·python
林爷万福2 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪
copyer_xyf2 小时前
Python 如何同时做很多事:进程、线程、协程
前端·后端·python
Full Stack Developme2 小时前
Spring Bean 依赖注入
python·spring·log4j
yuhuofei20212 小时前
【Python入门】Python中的元组tuple
python
Full Stack Developme2 小时前
Spring AOP 与 AspectJ
java·后端·spring