简体   繁体   中英

popup taking first entry of the table every time

I am having table with 6 fields and there is update button in the last field, onclick of that one popup appears but popup is taking only first entry of the in the table every time even if pressing the second or third entry's update button.

Here is my JSP Code `

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Report</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="col-sm-2"></div>
    <div class="col-sm-8">
        <div class="panel panel-primary">
            <div class="panel-heading">Details of Students</div>
            <div class="panel-body">

                <button id="newbtn" type="button"
                    class="form-control btn btn-success">Add New Student</button>

                    <button id="shwbtn" type="button"
                    class="form-control btn btn-success">show</button>

                <div style="margin-top: 40px;">

                    <table class="table table-bordered" id="updatedetailsFrm">
                        <thead>
                            <tr style="background-color: #E0E0E1;">
                                <th>Sr.No.</th>
                                <th>Name</th>
                                <th>Surname</th>
                                <th>Contact No</th>
                                <th>Email</th>
                                <th>Action</th>
                            </tr>
                        </thead>

                        <s:if test="noData==true">

                            <s:iterator value="beanList">
                                <tr>
                                    <td><s:property value="srNo" /></td>
                                    <td><s:property value="name" /></td>
                                    <td><s:property value="surname" /></td>
                                    <td><s:property value="contactno" /></td>
                                    <td><s:property value="email" /></td>

                                    <td>

                                        <a href="#popupLogin" data-rel="popup"
                                        data-position-to="window" id="updateBtnfirst"
                                        class="ui-btn ui-corner-all ui-shadow" data-transition="pop">Update</a>

                                        <div data-role="popup" id="popupLogin" data-theme="a"
                                            class="ui-corner-all">
                                            <form id="updatedetailsFrm" method="post">
                                                <div style="padding: 10px 20px;">
                                                    <h3>Update Your Data</h3>

                                                    <input id="name" placeholder="Name" type="text" name="name"
                                                        value='<s:property value="name"/>'>

                                                    <input id="surname" placeholder="Surname" type="text"
                                                        name="surname" value='<s:property value="surname"/>'>

                                                    <input id="contactno" placeholder="Contact No" type="text"
                                                        name="contactno" value='<s:property value="contactno"/>'>

                                                    <input id="email" class="disabled" placeholder="Email" type="text"
                                                        name="email" value='<s:property value="email"/>'>

                                                    <button value="update" type="button" id="updateBtn">Update</button> 
                                                </div>

                                            </form>
                                        </div>
                                        <button class="btn btn-danger" id="deleteBtn">Delete</button>
                                    </td>
                                </tr>
                            </s:iterator>
                        </s:if>
                    </table>

                    <s:else>
                        <div style="color: red;">No Data Found.</div>
                    </s:else>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-2"></div>

    <script>
        $("#updateBtn").click(function() {
            var id = document.forms["updatedetailsFrm"]["email"].value;
            var userName = document.forms["updatedetailsFrm"]["name"].value;
            var surName = document.forms["updatedetailsFrm"]["surname"].value;
            var contactNo = document.forms["updatedetailsFrm"]["contactno"].value;

            window.location = "updatedetails.action?email="+id+"&name="+userName+"&surname="+surName+"&contactno="+contactNo;

        })

//      $('#updateBtnfirst').click(function() {
//          document.getElementById("email").disabled = true;

//      })

        $('#newbtn').click(function() {
            window.location.href = "insert.jsp";
        })

        $('#shwbtn').click(function() {
            window.location = "report";
        })

        $("#deleteBtn").click(function() {
            var id = $(this).closest("tr").find('td:eq(4)').text();
            window.location.href = "deleterecord.action?&email=" + id;
        })

    </script>

</body>
</html>

`

So, my question is, popup should bring the row wise details in popup, in particular row.

Your iterator generates all of your rows with the same ID's over and over again. Update your iterator to generate unique ID's for each form in each row. I would then remove the ID from the update button and use a class instead so it's easy to bind your click event and use an HTML "data" attribute to hold the form ID value. Then when an update button is clicked, your JavaScript will be able to grab the data attribute value and then target the correct form via unique ID.

Example update button:

<button value="update" type="button" class="updateBtn" data-form-id="updatedetailsFrm-1">Update</button> 

Example form ID:

<form id="updatedetailsFrm-1" method="post">

With duplicate ID's, you'll only ever retrieve the first instance of what you're trying to target in the DOM. There are other ID's in your iterator you'll want to look at too. ID's should be unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM