Get text inside input with radiobutton input-group-addon

Asked

Viewed 152 times

0

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

inserir a descrição da imagem aqui

My html:

<label>Respostas:</label>
                        <div class="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 only getting the text of 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>

2 answers

1


To catch the input selected use:

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

And then climb a level using .parent() to get the next existing input

Example:

I was too lazy to carry 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>
                        <div class="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>

0

Based in this answer:

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

Browser other questions tagged

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