2016-07-25 18 views
0

Ich baue gerade ein Tabellensystem für eine Krebsumfrage und bin auf ein Problem mit Multiple-Choice-Filtern in einer neuen Tabellenzeile gestoßen. Siehe unten für ein Beispiel.Jquery Mehrfachselektion in wiederholender Tabelle mit Filtern

Bei der Auswahl von Lymphom, Leukämie und andere möchte ich, dass die Tabelle ein zusätzliches Eingabefeld mit jquery erstellt, damit der Benutzer Informationen eingeben kann. Dies funktioniert für die erste Zeile, aber beim Generieren einer zusätzlichen Zeile sind Probleme mit der zusätzlichen Benutzereingabe und die Benutzerauswahl erstellt keine zusätzlichen Auswahlfelder.

Ich habe das Problem in Codepen neu erstellt, jede Hilfe würde massiv geschätzt werden. https://codepen.io/seniorjono/pen/qNYYJp

EDITED: Ich habe das Original Javascript geändert haben, um jetzt nur eine statische Tabellenzeile zu erzeugen, ich die zusätzlichen Eingabefelder aber immer noch nicht meine Jquery bekommen auszulösen. (Siehe 'cancer2' im codepen)

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" /> 
<ol> 
    <section class="form_card"> 
     <li> 
      <h1>Lorem ipsum dolor sit amet</h1></li> 
     <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
     tempor incididunt ut labore et dolore magna aliqua.</h2> 
     <form id="pt_13" name="pt_13"> 
      <div class="content_split" style="width:25%;"> 
       <label class="check check--radio reveal_content">Yes 
        <input type="radio" id="pt_1_yes" name="radio" /> 
        <div class="check__indicator"></div> 
       </label> 
      </div> 
      <div class="content_split"> 
       <label class="check check--radio hide_content">No 
        <input type="radio" id="pt_1_no" name="radio" /> 
        <div class="check__indicator"></div> 
       </label> 
      </div> 
     </form> 
     <div class="hidden_content"> 
      <form id="pt_14" name="pt_14"> 
       <table width="100%" border="0" cellspacing="0" cellpadding="0" id="mans"> 
        <tr> 
         <td> 
          <div class="select_new" id="pt_13_1"> 
           <label> 
            <select> 
             <option selected disabled>Select Relation</option> 
             <option>Child</option> 
             <option>Father</option> 
             <option>Maternal grandfather</option> 
             <option>Materal grandmother</option> 
             <option>Paternal grandfather</option> 
             <option>Paternal grandmother</option> 
             <option>Sibling</option> 
            </select> 
           </label> 
          </div> 
         </td> 
         <td> 
          <div class="select_new" id="pt_13_2"> 
           <label> 
            <select class="cancer"> 
             <option selected disabled> Select Cancer</option> 
             <option value="Bladder">Bladder</option> 
             <option value="Breast">Breast</option> 
             <option value="Colorectal">Colorectal</option> 
             <option value="Kidney">Kidney</option> 
             <option value="Lung">Lung</option> 
             <option value="Leukemia">Leukemia</option> 
             <option value="Lymphoma">Lymphoma</option> 
             <option value="Melanoma">Melanoma</option> 
             <option value="MGUS">Multiple Myeloma/MGUS</option> 
             <option value="Myelodysplasia">Myelodysplasia/myelodysplastic syndrome</option> 
             <option value="Prostate">Prostate</option> 
             <option value="Other">Other</option> 
            </select> 
           </label> 
          </div> 
         </td> 
         <td class="cancer_optional"> 
          <div class="select_new Leukemia_type"> 
           <label> 
            <select> 
             <option selected disabled>Type of Leukemia</option> 
             <option>Acute myeloid leukemia (AML)</option> 
             <option>Chronic myeloid leukemia (CML)</option> 
             <option>Acute lymphocytic leukemia (ALL)</option> 
             <option>Chronic lymphocytic leukemia (CLL)</option> 
             <option>Plamsa cell leukemia</option> 
             <option>Other leukemia type</option> 
             <option>Unknown leukemia type</option> 
            </select> 
           </label> 
          </div> 
          <div class="select_new Lymphoma_type"> 
           <label> 
            <select> 
             <option selected disabled>Type of Lymphoma</option> 
             <option>Hodgkin lymphoma</option> 
             <option>Non-Hodgkin lymphoma (NHL)</option> 
             <option>Other lymphoma type</option> 
             <option>Unkown lymphoma type</option> 
            </select> 
           </label> 
          </div> 
          <div class="Other_type"> 
           <input type="text" class="other_input_text" id="Other_input_box" placeholder="Please Specify" required/> 
          </div> 
         </td> 
         <td><i class="fa fa-trash"></i> 
         </td> 
        </tr> 
       </table> 
       <div id="addTableRow" class="hidden_content"><i class="fa fa-plus"></i> Add family member</div> 
     </div> 
     </form> 
    </section> 
</ol> 

JS

// This section toggles the lymphoma/leukemia/other additional details 

$('.cancer').change(function() { 
    if ($(this).val() == 'Leukemia') { 
    $(this).closest('td').next('td').find('.Leukemia_type').delay(300).slideDown(); 
    $('.Lymphoma_type, #Other_type').slideUp();} 

    else if ($(this).val() == 'Lymphoma') { 
    $(this).closest('td').next('td').find('.Lymphoma_type').delay(300).slideDown(); 
    $('.Leukemia_type, .Other_type').slideUp();} 

    else if ($(this).val() == 'Other') { 
    $(this).closest('td').next('td').find('.Other_type').delay(300).slideDown(); 
    $('.Leukemia_type, .Lymphoma_type').slideUp(); 
    } 
else { 
    $('.Leukemia_type, .Lymphoma_type, .Other_type').slideUp() 
    } 
}); 



// Add Row to table with current details duplicated 

$(function() { 
    $('table').on('click', 'tr i', function(e) { 
     e.preventDefault(); 
     $(this).parents('tr').remove(); 
    }); 

    $("#addTableRow").click(function() { 
     $("#mans").each(function() { 
      var tds = '<tr>'; 
      jQuery.each($('tr:last td', this), function() { 
       tds += '<td>' + $(this).html() + '</td>'; 
      }); 
      tds += '</tr>'; 
      if ($('tbody', this).length > 0) { 
       $('tbody', this).append(tds); 
      } else { 
       $(this).append(tds); 
      } 
     }); 
    }); 
    }); 



// Reveal this section upon user selection of yes 

$(function() { 
    $('.reveal_content').change(function() { 
     $(this).parents(".form_card").find(".hidden_content").slideToggle(); 
    }); 
    $('.hide_content').change(function() { 
     $(this).parents(".form_card").find(".hidden_content").slideToggle(); 
    }); 
}); 

ich jede Hilfe dankbar würde, wie ich wirklich ganz im Moment stecke!

+0

gibt es mehrere Probleme mit dem JavaScript-Code. Ihr Problem besteht darin, dass Sie ein Änderungsereignis für dynamisch hinzugefügtes HTML ausführen. Die Lösung des Problems besteht darin, '$ ('. Cancer') zu ändern. Change (function() {' zu '$ ('. Hidden_content'). On ('change', '.cancer', function() { '. Aber das gibt Ihnen nächste Ebene des Problems, weil Sie HTML der vorhandenen Zeile auswählen, um neue – Fr0zenFyr

+0

hinzuzufügen Danke für das Fr0zenFyr, ich werde den Code ändern, so dass es nicht auf die vorhandene verweist und das könnte Teil der sein Lösung für mich! – seniorjono

+0

Danke, ich bezog mich auf Ihre Antwort, beachte auch, dass die Frage bearbeitet wurde. – seniorjono

Antwort

0
  1. Verwenden Delegation, wenn sie mit dynamischen HTML arbeiten (können Sie verstecken Elemente in dynamischen HTML zeigen)
  2. Verwenden Sie eine variable Zeile HTML zu speichern. Eine Alternative besteht darin, ein verstecktes Element in HTML zu verwenden, das (um sicherzustellen, dass alle Auswahlen in vorhandenem HTML - wie ausgewählte Option nicht in neue Zeile geklont wird)
  3. Toggle hilft nicht beim Ausblenden von Inhalten mit Ihren Optionsfeldern wie Sie wollen (in Original-Code, klicken Sie auf "Nein" zuerst für sich selbst gehen und sehen)

\t // This section toggles the lymphoma/leukemia/other additional details 
 
\t $('.hidden_content').on('change', '.cancer', function() { 
 
\t if ($(this).val() == 'Leukemia') { 
 
\t  $(this).closest('tr').find('.Leukemia_type').delay(300).slideDown(); 
 
\t  $(this).closest('tr').find('.Lymphoma_type, #Other_type').slideUp(); 
 
\t } else if ($(this).val() == 'Lymphoma') { 
 
\t  $(this).closest('tr').find('.Lymphoma_type').delay(300).slideDown(); 
 
\t  $(this).closest('tr').find('.Leukemia_type, .Other_type').slideUp(); 
 
\t } else if ($(this).val() == 'Other') { 
 
\t  $(this).closest('tr').find('.Other_type').delay(300).slideDown(); 
 
\t  $(this).closest('tr').find('.Leukemia_type, .Lymphoma_type').slideUp(); 
 
\t } else { 
 
\t  $(this).closest('tr').find('.Leukemia_type, .Lymphoma_type, .Other_type').slideUp() 
 
\t } 
 
\t }); 
 

 

 

 
\t // Add Row to table with current details duplicated 
 
\t $('table').on('click', 'tr i', function(e) { 
 
\t e.preventDefault(); 
 
\t $(this).closest('tr').remove(); 
 
\t }); 
 

 
\t var duplicate = "<tr>" + 
 
\t " <td>" + 
 
\t " <div id=\"pt_13_1\" class=\"select_new\">" + 
 
\t "  <label>" + 
 
\t "  <select>" + 
 
\t "   <option disabled=\"\" selected=\"\">Select Relation</option>" + 
 
\t "   <option>Child</option>" + 
 
\t "   <option>Father</option>" + 
 
\t "   <option>Maternal grandfather</option>" + 
 
\t "   <option>Materal grandmother</option>" + 
 
\t "   <option>Paternal grandfather</option>" + 
 
\t "   <option>Paternal grandmother</option>" + 
 
\t "   <option>Sibling</option>" + 
 
\t "  </select>" + 
 
\t "  </label>" + 
 
\t " </div>" + 
 
\t " </td>" + 
 
\t " <td>" + 
 
\t " <div id=\"pt_13_2\" class=\"select_new\">" + 
 
\t "  <label>" + 
 
\t "  <select class=\"cancer\">" + 
 
\t "   <option disabled=\"\" selected=\"\"> Select Cancer</option>" + 
 
\t "   <option value=\"Bladder\">Bladder</option>" + 
 
\t "   <option value=\"Breast\">Breast</option>" + 
 
\t "   <option value=\"Colorectal\">Colorectal</option>" + 
 
\t "   <option value=\"Kidney\">Kidney</option>" + 
 
\t "   <option value=\"Lung\">Lung</option>" + 
 
\t "   <option value=\"Leukemia\">Leukemia</option>" + 
 
\t "   <option value=\"Lymphoma\">Lymphoma</option>" + 
 
\t "   <option value=\"Melanoma\">Melanoma</option>" + 
 
\t "   <option value=\"MGUS\">Multiple Myeloma/MGUS</option>" + 
 
\t "   <option value=\"Myelodysplasia\">Myelodysplasia/myelodysplastic syndrome</option>" + 
 
\t "   <option value=\"Prostate\">Prostate</option>" + 
 
\t "   <option value=\"Other\">Other</option>" + 
 
\t "  </select>" + 
 
\t "  </label>" + 
 
\t " </div>" + 
 
\t " </td>" + 
 
\t " <td class=\"cancer_optional\">" + 
 
\t " <div class=\"select_new Leukemia_type\">" + 
 
\t "  <label>" + 
 
\t "  <select>" + 
 
\t "   <option disabled=\"\" selected=\"\">Type of Leukemia</option>" + 
 
\t "   <option>Acute myeloid leukemia (AML)</option>" + 
 
\t "   <option>Chronic myeloid leukemia (CML)</option>" + 
 
\t "   <option>Acute lymphocytic leukemia (ALL)</option>" + 
 
\t "   <option>Chronic lymphocytic leukemia (CLL)</option>" + 
 
\t "   <option>Plamsa cell leukemia</option>" + 
 
\t "   <option>Other leukemia type</option>" + 
 
\t "   <option>Unknown leukemia type</option>" + 
 
\t "  </select>" + 
 
\t "  </label>" + 
 
\t " </div>" + 
 
\t " <div class=\"select_new Lymphoma_type\">" + 
 
\t "  <label>" + 
 
\t "  <select>" + 
 
\t "   <option disabled=\"\" selected=\"\">Type of Lymphoma</option>" + 
 
\t "   <option>Hodgkin lymphoma</option>" + 
 
\t "   <option>Non-Hodgkin lymphoma (NHL)</option>" + 
 
\t "   <option>Other lymphoma type</option>" + 
 
\t "   <option>Unkown lymphoma type</option>" + 
 
\t "  </select>" + 
 
\t "  </label>" + 
 
\t " </div>" + 
 
\t " <div class=\"Other_type\">" + 
 
\t "  <input type=\"text\" required=\"\" placeholder=\"Please Specify\" id=\"Other_input_box\" class=\"other_input_text\">" + 
 
\t " </div>" + 
 
\t " </td>" + 
 
\t " <td><i class=\"fa fa-trash\"></i>" + 
 
\t " </td>" + 
 
\t "</tr>"; 
 

 
\t $("#addTableRowStatic").click(function() { 
 
\t $("tbody").append(duplicate); 
 
\t }); 
 

 

 

 
\t // Reveal this section upon user selection of yes 
 
\t function reveal() { 
 
\t $(".hidden_content").show('slideDown'); 
 
\t } 
 

 
\t function unreveal() { 
 
\t $(".hidden_content").hide('slideUp'); 
 
\t } 
 
\t $('#pt_13').change(function() { 
 
\t if ($('#pt_1_yes').is(':checked')) { 
 
\t  reveal(); 
 
\t } else { 
 
\t  unreveal(); 
 
\t } 
 
\t });
body { 
 
    background: #e3f2fd; 
 
} 
 
.Leukemia_type, 
 
.Lymphoma_type, 
 
.Other_type { 
 
    display: none; 
 
} 
 
.form_card, 
 
.form_card_result { 
 
    width: 80%; 
 
    max-width: 1150px; 
 
    margin: 50px auto; 
 
    border-radius: 3px; 
 
    box-shadow: 0 5px 5px rgba(0, 0, 0, .15); 
 
    color: #555; 
 
    font-family: Texta-Medium; 
 
    font-size: 1.5rem; 
 
    font-weight: 400; 
 
    background-color: #fff; 
 
    padding: 2em; 
 
    h1 { 
 
    font-family: Texta-Medium; 
 
    color: #555; 
 
    font-size: 2rem; 
 
    margin: 1em 0 0.5em; 
 
    text-decoration: underline; 
 
    span { 
 
     text-decoration: underline; 
 
    } 
 
    } 
 
    h2 { 
 
    font-family: Texta-Medium; 
 
    color: #0d47a1; 
 
    font-size: 1.5rem; 
 
    margin: 1em 0 1.5em; 
 
    span { 
 
     text-decoration: underline; 
 
    } 
 
    } 
 
    h5 { 
 
    font-family: TradeGothicLTStd-Cn18; 
 
    color: #0d47a1; 
 
    font-size: 3rem; 
 
    margin: 1em 0 1.5em; 
 
    font-weight: 300; 
 
    } 
 
    td { 
 
    padding: 1em 0; 
 
    } 
 
    .content_split { 
 
    display: inline-block; 
 
    height: 75px; 
 
    width: 50%; 
 
    p { 
 
     display: inline-block; 
 
    } 
 
    } 
 
    table { 
 
    color: #555; 
 
    .select_new { 
 
     width: 80%; 
 
     select { 
 
     width: 100%; 
 
     } 
 
    } 
 
    } 
 
    #addTableRow, 
 
    #addTableRow2, 
 
    #addTableRow3, 
 
    #addTableRow4, 
 
    #addTableRowStatic { 
 
    font-family: Texta-Medium; 
 
    cursor: pointer; 
 
    font-size: 1.2rem; 
 
    color: #555; 
 
    i { 
 
     vertical-align: middle; 
 
     padding-right: 10px; 
 
    } 
 
    } 
 
    .fa-trash { 
 
    cursor: pointer; 
 
    } 
 
} 
 
.hidden_content, 
 
.hidden_card { 
 
    display: none; 
 
} 
 
// Disables first delete row 
 
tr:nth-child(1) { 
 
    td i: nth-child(1) { 
 
    pointer-events: none; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" /> 
 
<ol> 
 
    <section class="form_card"> 
 
    <li> 
 
     <h1>Do you have a family history of cancer</h1> 
 
    </li> 
 
    <h2>If yes, please indicate the relatives relationship to you and the type of cancer each one of them was diagnosed with</h2> 
 
    <form id="pt_13" name="pt_13"> 
 
     <div class="content_split" style="width:25%;"> 
 
     <label class="check check--radio reveal_content">Yes 
 
      <input type="radio" id="pt_1_yes" name="radio" /> 
 
      <div class="check__indicator"></div> 
 
     </label> 
 
     </div> 
 
     <div class="content_split"> 
 
     <label class="check check--radio hide_content">No 
 
      <input type="radio" id="pt_1_no" name="radio" /> 
 
      <div class="check__indicator"></div> 
 
     </label> 
 
     </div> 
 
    </form> 
 
    <div class="hidden_content"> 
 
     <form id="pt_14" name="pt_14"> 
 
     <table width="100%" border="0" cellspacing="0" cellpadding="0" id="CancerTab"> 
 
      <tr> 
 
      <td> 
 
       <div class="select_new" id="pt_13_1"> 
 
       <label> 
 
        <select> 
 
        <option selected disabled>Select Relation</option> 
 
        <option>Child</option> 
 
        <option>Father</option> 
 
        <option>Maternal grandfather</option> 
 
        <option>Materal grandmother</option> 
 
        <option>Paternal grandfather</option> 
 
        <option>Paternal grandmother</option> 
 
        <option>Sibling</option> 
 
        </select> 
 
       </label> 
 
       </div> 
 
      </td> 
 
      <td> 
 
       <div class="select_new" id="pt_13_2"> 
 
       <label> 
 
        <select class="cancer"> 
 
        <option selected disabled>Select Cancer</option> 
 
        <option value="Bladder">Bladder</option> 
 
        <option value="Breast">Breast</option> 
 
        <option value="Colorectal">Colorectal</option> 
 
        <option value="Kidney">Kidney</option> 
 
        <option value="Lung">Lung</option> 
 
        <option value="Leukemia">Leukemia</option> 
 
        <option value="Lymphoma">Lymphoma</option> 
 
        <option value="Melanoma">Melanoma</option> 
 
        <option value="MGUS">Multiple Myeloma/MGUS</option> 
 
        <option value="Myelodysplasia">Myelodysplasia/myelodysplastic syndrome</option> 
 
        <option value="Prostate">Prostate</option> 
 
        <option value="Other">Other</option> 
 
        </select> 
 
       </label> 
 
       </div> 
 
      </td> 
 
      <td class="cancer_optional"> 
 
       <div class="select_new Leukemia_type"> 
 
       <label> 
 
        <select> 
 
        <option selected disabled>Type of Leukemia</option> 
 
        <option>Acute myeloid leukemia (AML)</option> 
 
        <option>Chronic myeloid leukemia (CML)</option> 
 
        <option>Acute lymphocytic leukemia (ALL)</option> 
 
        <option>Chronic lymphocytic leukemia (CLL)</option> 
 
        <option>Plamsa cell leukemia</option> 
 
        <option>Other leukemia type</option> 
 
        <option>Unknown leukemia type</option> 
 
        </select> 
 
       </label> 
 
       </div> 
 
       <div class="select_new Lymphoma_type"> 
 
       <label> 
 
        <select> 
 
        <option selected disabled>Type of Lymphoma</option> 
 
        <option>Hodgkin lymphoma</option> 
 
        <option>Non-Hodgkin lymphoma (NHL)</option> 
 
        <option>Other lymphoma type</option> 
 
        <option>Unkown lymphoma type</option> 
 
        </select> 
 
       </label> 
 
       </div> 
 
       <div class="Other_type"> 
 
       <input type="text" class="other_input_text" id="Other_input_box" placeholder="Please Specify" required/> 
 
       </div> 
 
      </td> 
 
      <td><i class="fa fa-trash"></i> 
 
      </td> 
 
      </tr> 
 
     </table> 
 
     <div id="addTableRowStatic" class="hidden_content"><i class="fa fa-plus"></i> Add family member</div> 
 
    </div> 
 
    </form> 
 
    </section> 
 
</ol>

+0

Fr0zenFyr du hast keine Ahnung wie sehr mir das geholfen hat, wenn du interessiert bist, bin ich mehr als glücklich, ein wenig Geld für ein Bier oder einen Kaffee einzustecken, wenn yo Du hast einen Spendenknopf, um dir zu danken, danke, dass du bei mir geblieben bist. – seniorjono

+0

@ user2361983: Danke für deinen Ausdruck :) Ich werde irgendwann ein Bier mit dir nehmen. für jetzt, nur ein upvote auf meine antwort und akzeptieren es als lösung wird tun. Prost! – Fr0zenFyr