Can you capture a value from the radio button via ajax

0
<div class="btn-group btn-group-toggle col-xs-5" data-toggle="buttons">
            <button type="radio" name="tipoTransacao" id="tipoTransacao" value="1" class="btn btn-success" ><i class='fa fa-arrow-up'></i>Entrada</button>
            <button type="radio" name="tipoTransacao" id="tipoTransacao" value="2" class="btn btn-danger">Saída<i class='fa fa-arrow-down'></i>Saída</button>
          </div>

And I'm trying to capture it like this:

function salvar(e){
e.preventDefault();
var fk_idConta = $(this).find('select[name=fk_idConta]').val();

var tipoTransacao =  $("button[name='tipoTransacao']:checked").val();
var demo8 = $(this).find('input[name=demo8]').val();
var dataTransacao = $(this).find('input[name=dataTransacao]').val();
var meioTransacao = $("input[name='meioTransacao']:checked").val();
var descTransacao = $(this).find('textarea[name=descTransacao]').val();
    
asked by anonymous 16.11.2018 / 06:18

1 answer

1

Elements <button> do not have value . Even if you declare the values in the HTML, they will not exist in JavaScript. The "correct" way to use radio buttons, is to give name different to them, and then validate by name, not by value.

However, if you absolutely must work in this way, you can get the value of value directly from the attribute declared in HTML with $("button[name='tipoTransacao']:checked").attr('value')

    
16.11.2018 / 06:46