Problem recovering 2 values with javascript and jquery

Asked

Viewed 45 times

0

I have the problem to recover 2 values and send by load. I can recover the data from the data attribute link but the input I can’t recover to concatenate with the data and do the right thing. Follows the code.

Javascript:

$('.buscarr').click(function() {
    var id = $(this).attr('campo1');
    var id2 = $(this).attr('data');
    alert(id);
    console.log(id);
    console.log(id2);
    $("#cliente1").load($(this).attr(id2 + id));
});

HTML:

<div class="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>
  • I don’t understand, if you already know the id, why seek the value of id? What you intend to store here: var id = $(this).attr('campo1');?

  • the value of the field is concatenate with another value.

  • But here var id = $(this). attr('campo1'); does not return me anything.

  • It returns nothing because attr search for an element attribute, and buscarr doesn’t have it.

  • I had tried with attribute tmb is nothing , then I returned to id. Continues returning me 'Undefined'.

  • Obg was able to recover the id value but I can’t load concatenating $("#cliente1"). load($(this).attr(id2+id)); sera pq ?. I’m trying.

Show 1 more comment

1 answer

0


It returns nothing because attr searches for an attribute of the element, and buscarr does not have it. - Lucas Costa

Use var id = var id = $('#campo1').val(); or var id = $('form-control').val();

$(document).ready(function(){
$('.buscarr').click(function() {
    var id = $('#campo1').val();
    //var id = $('form-control').val();
    var id2 = $(this).attr('data');
    //alert(id);
    console.log(id);
    console.log(id2);
    //$("#cliente1").load($(this).attr(id2 + id));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>

You can also use var id = document.getElementById('campo1').value;

$(document).ready(function(){
$('.buscarr').click(function() {
    var id = document.getElementById('campo1').value;
    var id2 = $(this).attr('data');
    //alert(id);
    console.log(id);
    console.log(id2);
    //$("#cliente1").load($(this).attr(id2 + id));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>

  • Leo obg. But I’m trying to load and I’m not getting it, I made this code snippet $("#cliente1"). load('/wbahd/servico_servlet?acao=search&search='+id); it does the action takes the parameter but does not load the page.

  • edited the answer, I think one of them will suit you

  • nothing yet I need to load the pargina

  • there is already another problem, which deserves another question so that the answer/comments regarding the current question does not become meaningless

Browser other questions tagged

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