嗨,我的系统由一个Spring Maven项目组成,它包含一个父母列表,并通过 th: 使用百里香模板引擎在html页面中显示 :每个 问题是它只能在第一个按钮上工作,而我尝试过的其余按钮则不工作下面的代码
脚本
$(document).ready(function(){ $("#button").attr("name").click(function(){ alert($(this).attr("name")); }); });
带有百里香叶迭代器的表
<table class="table"> <tr> <th>Parent Name</th> <!-- <th>Country</th> --> <!-- <th>State</th> <th>District</th> --> <th>Address</th> <th>Phone No</th> <th>Email</th> <th>Active/Inactive</th> <th></th> </tr> <tr th:each=" parent : ${parentList}"> <td th:text="${parent.parentName}"></td> <!-- <td th:text="${parent.district.state.country.countryName}"></td> --> <!-- <td th:text="${parent.district.state.stateName}"></td> <td th:text="${parent.district.districtName}"></td> --> <td th:text="${parent.parentAddress}"></td> <td th:text="${parent.parentPhone}"></td> <td th:text="${parent.parentEmail}"></td> <td><a id="button" href="#" class="btn btn-small" th:value="${parent.id}" th:name="${parent.id}" th:text="${parent.id}"></a></td> </tr> </table>
您的代码有两个问题。首先,您为按钮使用#ID,这应该是一个类,例如.button。
其次,选择器和点击之间的属性有些奇怪。试试下面的代码
$(".button").click(function(){ alert($(this).attr("name")); });
jQuery对匹配的元素进行隐式迭代。
希望这可以帮助!