Take the value of a <TD> tag by jquery and send to Controller method

Asked

Viewed 1,245 times

1

How do I take the value of a tag when I double-click it and take that value and move to a method in my controller. Double click function is working.

$('#nmUsuario').on("dblclick", '.clique', function() {
    CarregaDados();
})

Where nmUsuario is the tbody of my table. My method CarregaDados();is where I want to take the value of the tag clicked and move to the controller via json.

function CarregaDados() {

    $('.rowid').find('td').each(function () {
        var test = $(this).text();
        alert(test);
    })


    $.ajax({

        url: '/CadastroAcesso/CarregaDadosPagina',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ aqui não sei o que passar }),
        success: function (data) {
            alert('Alô, tudo bem?');
        },
        error: function (error) {
        }
    })
}

Where rowid is mine <TR>, however, as I just want to get the tag clicked, it makes sense to make a each? The code I made before ajax, does not enter Alert. I don’t know the test value.

public JsonResult CarregaDadosPagina(string _nivel, string _nome, string _usuario)
        {
            RupturaEntities db = new RupturaEntities();

            //var result_carrega_pagina = db.

            return Json(new {  }, JsonRequestBehavior.AllowGet);
        }
  • What does the controller expect to receive? A string list?

  • I will edit and display my controller method. It is a string parameter.

  • It would be interesting to add your HTML.

2 answers

3

You can know which td was clicked using "this" within the callback function:

$('table td').on("dblclick", '.clique', function() {
    alert($(this).text());
})

Another example with ajax:

$('table td').on("dblclick", '.clique', function() {
    var dados = {
        textoTd : $(this).text()
    };

    $.ajax({
        url: '/CadastroAcesso/CarregaDadosPagina',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: dados,
        success: function (data) {
            alert('Alô, tudo bem?');
        },
        error: function (error) {
        }
    })
})

1


Problem solved by another colleague:

$('#nmUsuario').on("dblclick", ".clique", function() {
   CarregaDados($(this).text());
});
function CarregaDados(ajaxParameter) {

     $.ajax({

        url: '/CadastroAcesso/CarregaDadosPagina',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
         data: { nome: ajaxParameter },
        success: function (data) {
            alert('Alô, tudo bem?');
        },
        error: function (error) {
            alert('Fail?');
        }
    })
}

Jsfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.