How can I get the ID of an element using jQuery?

后端 未结 19 2583
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 11:57

Why doesn\'

相关标签:
19条回答
  • 2020-11-22 12:07

    Above answers are great, but as jquery evolves.. so you can also do:

    var myId = $("#test").prop("id");
    
    0 讨论(0)
  • 2020-11-22 12:07

    $('#test').attr('id') In your example:

    <div id="test"></div>
    
    $(document).ready(function() {
        alert($('#test').attr('id'));
    }); 
    
    0 讨论(0)
  • 2020-11-22 12:13

    This can be element id , class , or automatically using even

    ------------------------
    $(this).attr('id');
    =========================
    ------------------------
    $("a.remove[data-id='2']").attr('id');
    =========================
    ------------------------
    $("#abc1'").attr('id');
    =========================
    
    0 讨论(0)
  • 2020-11-22 12:19

    $('#test') returns a jQuery object, so you can't use simply object.id to get its Id

    you need to use $('#test').attr('id'), which returns your required ID of the element

    This can also be done as follows ,

    $('#test').get(0).id which is equal to document.getElementById('test').id

    0 讨论(0)
  • 2020-11-22 12:19
    <html>
    <head>
      <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>
    <?php
        // include Database connection file 
        include("db_connection.php");
    
        // Design initial table header 
        $data = '<table class="table table-bordered table-striped">
                            <tr>
                                <th>No.</th>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Email Address</th>
                                <th>Update</th>
                                <th>Delete</th>
                            </tr>';
        $query = "SELECT * FROM users";
        if (!$result = mysqli_query($con, $query)) {
            exit(mysqli_error($con));
        }
        // if query results contains rows then featch those rows 
        if(mysqli_num_rows($result) > 0)
        {
            $number = 1;
            while($row = mysqli_fetch_assoc($result))
            {
                $data .= '<tr>
                    <td>'.$number.'</td>
                    <td>'.$row['first_name'].'</td>
                    <td>'.$row['last_name'].'</td>
                    <td>'.$row['email'].'</td>
                        <td><button onclick="DeleteUser('.$row['id'].')" class="btn btn-danger">Delete</button>
                    </td>
                </tr>';
                $number++;
            }
        }
    
        else
        {
            // records now found 
            $data .= '<tr><td colspan="6">Records not found!</td></tr>';
        }
    
        $data .= '</table>';
        echo $data;
    ?>
    
    <script type="text/javascript">
    
        function DeleteUser(id) {
        var conf = confirm("Are you sure, do you really want to delete User?");
        if (conf == true) {
            $.ajax({
                        url:'deleteUser.php',
                        method:'POST',
                        data:{
                            id:id
                        },
                success:function(data){
                          alert('delete successfully');
                       }
    
    
    
    
    }
    });
    
    deleteUser.php
    
    <?php
    // check request
    if(isset($_POST['id']) && isset($_POST['id']) != "")
    {
        // include Database connection file
        include("db_connection.php");
    
        // get user id
        $user_id = $_POST['id'];
    
        // delete User
        $query = "DELETE FROM users WHERE id = '$user_id'";
        if (!$result = mysqli_query($con, $query)) {
            exit(mysqli_error($con));
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 12:19

    Important: if you are creating a new object with jQuery and binding an event, you MUST use prop and not attr, like this:

    $("<div/>",{ id: "yourId", class: "yourClass", html: "<span></span>" }).on("click", function(e) { alert($(this).prop("id")); }).appendTo("#something");

    0 讨论(0)
提交回复
热议问题