Actually, the post method

$(document).ready(function(){ $('#registerButton').on('click',function(){ var lot1 = $('#text').val(); $.post( 'obrabotchik.php', // адрСс Ρ„Π°ΠΉΠ»Π° с ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠΌ {lot:lot1}, function (data){ $('#otvet').html(data); }); }); }) 

got a sample of radio paths

 <input type="radio" id="inputReg" value="One"> <input type="radio" id="inputReg" value="Two"> 

When selecting a radio button, it is necessary to immediately transfer the value to the following form.

 <input type="text" id="GR" value="Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ€Π°Π΄ΠΈΠΎΠΊΠ½ΠΎΠΏΠΊΠΈ"> 

I wanted to use the following function

     $ ("#inputReg") .on ("click", function () {
         // copy the value of the radio button to the form
        ("#GR") .val ($ ("input: checked") .val ());
     });

It works only if the radio input was on the page initially, but it is loaded ignored.

How to change the last function to copy values ​​from a loaded object?

  • The "on" method will hang events and dynamically added items. Why do you have several elements with one id? Try using the class name instead of id; id should be unique on the page. - jekaby
  • Thus (one id) I separate the radio route packet. There are others ... I am interested in the value of only one input with the value of checked. - Xstroy
  • @Xstroy id is a unique identifier, elements on the page must be with a unique id . You can separate the package for example one class. And the elements in the group are separated by one name. - Alex
  • It seems to me, no matter what will be the anchor for the function. - Xstroy
  • I see in my example that the id was really different, and there the event was hung on all the inputs. How to specify the name? - Xstroy

1 answer 1

For dynamically created elements, you should hang the event on the document or body

 $(document).on("click", ".inputReg", function() { //ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ значСния Ρ€Π°Π΄ΠΈΠΎΠΊΠ½ΠΎΠΏΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΡƒ $("#GR").val($("input:checked").val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <input type="radio" name="test" id="inputReg" class="inputReg" value="One"> <input type="radio" name="test" id="inputReg" class="inputReg" value="Two"> <input type="text" name="GR" id="GR"> 

PS: in this case it is more correct to hang the event on the class, however if you need to hang the event on id , the code will also work.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin ♦