csdn产品小助手 2018-11-05 05:56 采纳率: 0%
浏览 7

调用html id然后上课

Is it possible to call an html class with specific id, because for now I have same functionality in my class then I need to pull it through class + id to be specific.

I have button that populates data to my table:

<button type="button" onclick="loadData()">Get Data</button>

and I have two tables with the same class name:

<table class="sortable" id="1">
    <thead>
        <tr>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>    
        <tr>
            <td></td>
            <td></td>
        </tr>

    </tbody>
</table>


  <table class="sortable" id="2">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>    
        <tr>
            <td></td>
            <td></td>
        </tr>

    </tbody>
</table>

My aim is to update only the first table which has the id of 1. because right now when I clicked the button both of them will update since they have the same class name. Is it possible if they can Identify by class name + id?

here is my update:

 function loadData() {        

 $.ajax({
        url: '/Home/ReadDB',
        type: 'GET',

    dataType: 'json',
    success: function (data) {
        var row = '';
        $.each(data, function (i, item) {
            row += '<tr><td>' + item.LName + '</td><td>' + item.age
                + '</td><td>' + '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Add </button >' + '</td></tr>';

        });
        console.log(row)
        $('.sortable tbody').html(row); // --This one overrides my previous result and want to identify by id            
    },
    error: function (jqXhr, textStatus, errorThrown) {
        alert(errorThrown);
    }
});

}
  • 写回答

3条回答 默认 最新

  • 衫裤跑路 2018-11-05 06:05
    关注

    Simple:

    var firstTable = $('.sortable[id="1"]');
    console.log(firstTable);
    
    评论

报告相同问题?