调用html id然后上课

时间:2018-11-05 05:56:23

标签: html ajax

是否可以用特定的id调用html类,因为现在我在类中具有相同的功能,因此我需要通过class + id来将其拉出。

我有一个将数据填充到表中的按钮:

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

并且我有两个具有相同类名的表:

<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>

我的目的是仅更新ID为1的第一个表,因为现在当我单击按钮时,由于它们具有相同的类名,因此它们两个都将更新。他们是否可以通过类名+ ID进行标识?

这是我的更新:

 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 个答案:

答案 0 :(得分:0)

简单:

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

答案 1 :(得分:0)

首先更改您的ID名称,请阅读此答案https://stackoverflow.com/a/70586/2724173

然后像这样将它们连接起来

$('.sortable#IDname tbody').html(row);

答案 2 :(得分:0)

尝试一下

$.('#1.sortable tbody').html(row);