samedi 1 août 2015

Problems with a questionnaire cloned

I´m doing a questionnaire that can be clone using Jquery .clone(). I´m using checkboxes to answer the questions using yes or no. My questionnaire is inside a table and then using this code:

  $(':checkbox').on('change',function(){
        var tr = $(this), name = tr.prop('name');

        if(tr.is(':checked')){
            $(this).closest('tr').find('input[type=checkbox]').not($(this)).prop('checked', false);
        }

I can check only one checkbox in the same row(tr), but the problem is when I clone the questionnaire using a button. The script is working for the first questionnaire, but not for the second. When is cloned is generated a new id (previous_id_2).

I tried many things but I think that the script is not the most suitable for my code, because only works with the first. I need something that works as follows:

If two checkboxes in same row -> only be able to select one. 

>And something that should be works for different div id, because it´s generated
 a new id for each table, the structure of the html is:
 <div id="questionnaire {{ number }}">
 <table class="table quest">
 <thead></thead>
 <tbody>
 <tr>
 <td><checkbox 1 id="q_Q1_y{{ number }}" name="q_Q1_y{{ number }}" class="table quest yes" ></td>
 <td><checkbox 2 id="q_Q1_n{{ number }}" name="q_Q1_n{{ number }}" class="table quest"></td>
 </tr>
 <tr>
 <td><checkbox 1 id="q_Q2_y{{ number }}" name="q_Q1_y{{ number }}" class="table quest yes"></td>
 <td><checkbox 2 id="q_Q2_n{{ number }}" name="q_Q2_n{{ number }}" class="table quest"></td>
 </tr>
 </tbody>

number shows the number of repetitions that you have.I know that radio buttons are most suitable for this, but I need do it suing checkboxes. I think the script should not be more complicated than I did.




Aucun commentaire:

Enregistrer un commentaire