Get text inside input with radiobutton input-group-addon

0

I have this form with these inputs, I want to get the value that is inside the input of the selected radio button, example as in the image, I should get "bb".

Myhtml:

<label>Respostas:</label><divclass="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta">
                            </span>
                            <input type="text" class="form-control" id="pergunta1">
                        </div><!-- /input-group -->
                        <div class="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta">
                            </span>
                            <input type="text" class="form-control" id="pergunta2" >
                        </div><!-- /input-group -->
                        <div class="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta" >
                            </span>
                            <input type="text" class="form-control" id="pergunta3">
                        </div><!-- /input-group -->

My jquery so far, I'm just getting text from all fields:

<script>
    $(document).ready(function(){
        $('#btnenviarnoticia').click(function() {
            var btn = $(this);
            btn.button('loading');
            var respostas = [$('#pergunta1').val(),$('#pergunta2').val(),$('#pergunta3').val()];
            alert(respostas);

        });
    });
</script>
    
asked by anonymous 17.01.2018 / 14:49

2 answers

1

To get the input selected use:

$('input[name="respostacorreta"]:checked')

And then go up one level using .parent() to get the next input that exists

Example :

I was too lazy to load Bootstrap

$('button').click(function(){
    var resposta = $('input[name="respostacorreta"]:checked').parent().next('input').val();
    alert(resposta);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>Respostas:</label><divclass="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta">
                            </span>
                            <input type="text" class="form-control" id="pergunta1">
                        </div><!-- /input-group -->
                        <div class="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta">
                            </span>
                            <input type="text" class="form-control" id="pergunta2" >
                        </div><!-- /input-group -->
                        <div class="input-group">
                            <span class="input-group-addon">
                                <input type="radio" name="respostacorreta" >
                            </span>
                            <input type="text" class="form-control" id="pergunta3">
                        </div><!-- /input-group -->
                        
             
 <br>
<button>Ação!</button>
    
17.01.2018 / 14:56
0

Based on in this answer :

$('input[name=radioName]:checked', '#myForm').val()
    
17.01.2018 / 14:57