Hello to all! There is such a simple code.

<input type="radio" class="rd_button" id="rad_id" name="rad" value="1">Часто встречаю в категориях несоответствующие ей товары <br> <input type="radio" class="rd_button" id="rad_id" name="rad" value="2">Очень много категорий, я не могу найти нужную <br> <input type="radio" class="rd_button" id="rad_id" name="rad" value="3">Я не понимаю, как сортировать товары, как пользоваться фильтрами <br> <input type="radio" class="rd_button" id="rad_id" name="rad" value="4">Мне не хватило фильтров, чтобы отсортировать нужный товар <br> <input type="radio" class="rd_button" id="rad_id" name="rad" value="5">Фильтр «Регион» показывает компании не из моего региона <br> <input type="radio" class="rd_button" id="rad_id" name="rad" value="6">При поиске мне выдает не те товары, которые я искал(а) 

6 radiobutton buttons.

Then I want to send a value to the server using an ajax request, but for some reason I never choose which value of radiobutton I’ve selected to be transferred to 1 (value = 1). Tell me what could be the problem?

This is how I transmit the data:

 $.ajax( { type: 'POST', url: 'handler.php', data: "msg2="+$('#msg2_id').val()+"&rad="+$('#rad_id').val(), success: function(container).... 

    2 answers 2

     $("body").change(function () { $("output").text($("input[name='rad']:checked").val()) }) 
     label { cursor: pointer; } label:after { content: ""; display: block; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <label><input type="radio" name="rad" value="1">Часто встречаю в категориях несоответствующие ей товары</label> <label><input type="radio" name="rad" value="2">Очень много категорий, я не могу найти нужную</label> <label><input type="radio" name="rad" value="3">Я не понимаю, как сортировать товары, как пользоваться фильтрами</label> <label><input type="radio" name="rad" value="4">Мне не хватило фильтров, чтобы отсортировать нужный товар</label> <label><input type="radio" name="rad" value="5">Фильтр «Регион» показывает компании не из моего региона</label> <label><input type="radio" name="rad" value="6">При поиске мне выдает не те товары, которые я искал(а)</label> <hr> <output></output> 

    • Oh, thank you kind man! Earned! - rekrut
    • @rekrut, then check the answer with a tick on the left. - Qwertiy

    First, read about the identifiers:

    In the document code, each identifier is unique and must be included only once.

    Work RadioButton :

    var rad = document.getElementsByName('rad');

    After running the getElementsByName command, the rad variable contains an array of radiobutton elements. That is why further work with this variable should be conducted as with an array. In the following lines, we iterate over each element of the array and check if it is selected.

     for (var i=0; i<rad.length; i++) { if (rad[i].checked) { alert(rad[i].value); } }