简体   繁体   中英

Cloning a section with JQuery not working

I try to clone a section after another section on user input (clicking a radio button), but it doesn't works... your help is appreciated.

<section class="clonetester">
    <input id="1a" type="radio" value="1" name="q1">yes
    <input id="1b" type="radio" value="0" name="q1" >no
    <br />
    <input id="date1" type="datetime-local" name="date" />date<br />
</section>

<section class="here">clone follows</section>

<script>
$('input').click(function(e){
    $('#1a').(':checked'){
        $('.clonetester').clone().appendTo(".here");
    }
})
</script>

You have multiple changes that you will have to make for it to work.

  • Use change instead of click for input elements.
  • $('#1a').(':checked') has to be replaced with $('#1a').is(':checked') and should be enclosed in a if block.

 $('body').on('change', 'input[type="radio"]', function() { var $this = $(this); if ($this.hasClass('1a') && $this.is(':checked')) { // closeset clonetester var $clonetester = $this.closest('.clonetester').first(); if ($clonetester) { $clonetester.clone().appendTo(".here"); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section class="clonetester"> <input class="1a" type="radio" value="1" name="q1">yes <input class="1b" type="radio" value="0" name="q1">no <br /> <input class="date1" type="datetime-local" name="date" />date <br /> </section> <section class="here">clone follows</section> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM