2016-04-20 10 views
0

DEMOAttribut-ID Selector funktioniert nicht auf 2. Klicken Sie

Hallo, Ich habe fieldset, die von Drupal erzeugt, auf jeder Ajax Request ID/Klasse nicht konstant :-(sein wird

Was Iam tun? Ich versuche, die ausgewählten Werte bei Änderung später zu erfassen, ich werde valdiation hinzufügen, dies funktioniert für einen senorio.Das Problem kommt, wenn ich versuche, "Kind hinzufügen" (kann bis zu 10 Kinder hinzufügen) und wählen Sie die Werte "Monat , Tag, Jahr "werden die gleichen vorhandenen Werte angezeigt. Stattdessen werden die alten Werte des neuen Werts aufgefüllt.

Schätzen Sie Ihre Hilfe! Dank

JS:

$(function(){ 
    var registrationDay, registrationMonth, registrationYear; 
    var onchangeId = $("[id*='edit-field-enr-do-you-have-childrn-ph-und-0-field-enr-child-birthdate-und-0-value']"); 

console.log("--- " + onchangeId) 

$(onchangeId).parents('.group-enr-baby-information').on('change', function(e) { 
    registrationMonth = $("[id*='edit-field-enr-do-you-have-childrn-ph-und-0-field-enr-child-birthdate-und-0-value-month']").val(); 
    registrationDay  = $("[id*='edit-field-enr-do-you-have-childrn-ph-und-0-field-enr-child-birthdate-und-0-value-day']").val(); 
    registrationYear = $("[id*='edit-field-enr-do-you-have-childrn-ph-und-0-field-enr-child-birthdate-und-0-value-year']").val(); 

    alert ("Month - " + registrationMonth + " Day - " + registrationDay + " Year - " + registrationYear); 
    console.log (registrationDay + registrationMonth + registrationYear); 
}); 

    $('.addChild').on('click', function(){ 
    $('.child').show(); 
}) 

})

+0

wird es das gleiche sein, weil Ihr die gleiche ID für die Konsole verwenden. – guradio

+0

@guradio aber ich habe globale Variablen definiert, muss es auf Änderung aktualisieren, ich glaube, –

+0

Sie müssten Ihre Änderung Event für die Elemente, nachdem sie existieren. Sie führen es vor und dann erstellen Sie die Elemente. – PHPglue

Antwort

0

Ich habe dieses Problem wurde behoben, durch Elternklasse im Einklang jeder Schleife zu tun und Wert bekommen!

DEMO

(function($) { 
    $(function() { 
     var getMonth, getDay, getYear, getDate; 
     $(document).on('change', '#node_enfamama_registration_form_form_group_enr_hide_child_info .form-select', function() { 
      $(this).each(function() { 
       if ($(this).parents().hasClass('date-month')) { 
        getMonth = $(this).val(); 
       } else if ($(this).parents().hasClass('date-day')) { 
        getDay = $(this).val(); 
       } else if ($(this).parents().hasClass('date-year')) { 
        getYear = $(this).val(); 
        getDate = getMonth + getDay + getYear; 
        $('.greater-msg').remove(); 
        $('.less-then-msg').remove(); 

        var dob = new Date(getYear + "-" + getMonth + "-" + getDay); 
        var today = new Date(); 
        var age = Math.floor((today - dob)/(365.25 * 24 * 60 * 60 * 1000)); 
        if (age => 3) { 
         $(this).parents('.fieldset-wrapper').after('<div class="greater-msg">You can also visit <a href="http://www.enfagrow4.com" target="_blank">www.enfagrow4.com</a> to know how you can keep giving your child the 360 advantage.</div>') 
        } else if (age <= -1) { 
         //$(this).parents('.fieldset-wrapper').after('<div class="less-then-msg">Less Disclaimer: In compliance with EO51, Mead Johnson Nutrition cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>') 
        } else if (age <= 3) { 
         $(this).parents('.fieldset-wrapper').after('<div class="less-then-msg">Less Disclaimer: In compliance with EO51, Mead Johnson Nutrition cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>') 
        } else { 
        } 

       } else {} 

      }); 
     }); 
    }); 
})(jQuery);