单击元素'<a>&#39; inside table is not working

时间:2018-12-04 18:28:29

标签: javascript html

My code is not recognizing the click on <a href="javascript:void(0);">Inserir</a> element inside a HTML Table that are populate dynamically. I dont know what is wrong, because I have already use this code in others javascript function. Please, can anyone helps me?

My table body is populate dynamically with this script:

function atualizarTabelaInserirPnEmbalagem() {
    $.ajax({
        type: "POST",
        async: true,
        url: urlInit + "/Quotation/Check_Item_Embalagem",
        data: { RefCotacao: $('#refCotacao').val() },
        success: function (data) {
            if (data.found) {
                $("#tableEid-body").empty();
                var itensEmbalagem = data.itensEmb;
                var partN = data.infoC;
                var qtds = data.qtdItens;
                var id = $("#idEmbalagemPN")[0].innerText;
                for (var i = 0; i < partN.length; i++) {
                    var qtdNestaEmbalagem = 0;
                    var partNumber = partN[i].PartNumber;

                    try {
                        if (itensEmbalagem[i].Qtd !== null && itensEmbalagem[i].IDEmbalagem == id) {
                            qtdNestaEmbalagem = itensEmbalagem[i].Qtd;
                        }
                    } catch (e) {
                        qtdNestaEmbalagem = 0;
                    }
                    
                    var qtdSolicitada = partN[i].Qtd;

                    var body = $("#tableEid-body");
                    var bodyRow = "";
                    bodyRow += "<tr>"
                    bodyRow += "<td><center>" + partNumber + "</center></td>"
                    bodyRow += "<td><center>" + qtdSolicitada + "</center></td>"
                    bodyRow += "<td><center>" + qtdNestaEmbalagem + "</center></td>"
                    bodyRow += "<td><center>" + qtds[i] + "</center></td>"
                    bodyRow += "<td><center>" + "<input type=" + "number" + " class=" + "form-control" + " />" + "</center></td>"
                    bodyRow += "<td>" + "<a href=" + "javascript:void(0);" + ">Inserir</a>" + "</td>"
                    bodyRow += "</tr>";
                    body.append(bodyRow);
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Error1 - " + textStatus)
            alert("Error2 - " + jqXHR.responseText)
            alert("Error3 - " + errorThrown)
        }
    });
}

Here is my table in HTML with all Headers inside thead element:

                <table class="tableEmbalagem table table-hover table-bordered" id="tableEid" style="background-color: white !important">
                    <thead>
                        <tr>
                            <th>
                                <center>
                                    PART NUMBER
                                </center>
                            </th>
                            <th>
                                <center>
                                    QUANTIDADE SOLICITADA
                                </center>
                            </th>
                            <th>
                                <center>
                                    QUANTIDADE NESTA EMBALAGEM
                                </center>
                            </th>
                            <th>
                                <center>
                                    QUANTIDADE TOTAL INSERIDA
                                </center>
                            </th>
                            <th>
                                <center>
                                    QUANTIDADE A INSERIR
                                </center>
                            </th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody id="tableEid-body"></tbody>
                </table>

And here is my script to click in <a href="javascript:void(0);">Inserir</a> element:

$(".tableEmbalagem tbody tr td a").on('click', function (e) {
    //get Index of row
    var rowIndex = $(this).closest('tr').index();
    var qtdPN = "";

    //get qtd inside embalagem
    $(this).closest('tr').find("input").each(function () {
        qtdPN = this.value;
    });

    //get PN
    var table = document.getElementById('tableEid');
    var PN = table.rows[rowIndex].cells[0].innerText.trim();

    //get embalagem ID
    var idEmbalagem = $('#idEmbalagemPN').text();

    //get refCotacao
    var refCotacao = $('#refCotacao').val();

    $.ajax({
        type: 'POST',
        url: urlInit + '/Quotation/saveItemEmbalagem',
        data: {
            PN: PN,
            qtdPN: qtdPN,
            idEmbalagem: idEmbalagem,
            refCotacao: refCotacao,
        },
        processData: true,
        success: function (response) {
            alert(response.message);
        },
    });
});

0 个答案:

没有答案