使用jQuery从列表中获取点击的按钮ID [英] get clicked button id from a list using jquery

查看:95
本文介绍了使用jQuery从列表中获取点击的按钮ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统包含一个Spring Maven项目 它包含一个父母列表,并使用thymeleaf模板引擎通过 th:each 在html页面中显示 问题是它只能在第一个按钮上工作,而不能在按钮的其余部分上工作 我尝试了以下代码

hi my system consist of a spring maven project it contains a list of parents and it is showed in html page using thymeleaf template engine via th:each the problem is it work on the first button only it does not working rest of the button i tried below code

脚本

$(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,这应该是一个类,例如按钮.

There are a couple of problems with your code. First of all you are using an #ID for your button, this should be a class e.g. .button.

第二,选择器和点击之间的属性有些奇怪.尝试以下代码

Second, the attr between the selector and the click is a bit weird. Try the following code

  $(".button").click(function(){
       alert($(this).attr("name"));
  });

jQuery对匹配的元素进行隐式迭代.

jQuery iterates implicitly over matched elements.

希望这会有所帮助!

这篇关于使用jQuery从列表中获取点击的按钮ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆