属性ID选择器在第二次单击时不起作用

时间:2016-04-20 09:19:45

标签: javascript jquery ajax drupal fieldset

DEMO

您好, 我有从Drupal生成的fieldset,每个Ajax请求ID / Class都不是常量:-(

我在做什么? 我试图捕捉选定的变化值后来我将添加valdiation,这适用于一个senorio。当我尝试“添加孩子”(最多可以添加10个孩子)并选择值“月,日,年”时,问题就出现了,显示了相同的现有值。而是填充New Value旧值。

感谢您的帮助! 谢谢

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();
})

})

1 个答案:

答案 0 :(得分:0)

我已修复此问题, 保持父类我正在做每个循环并获得价值!

<强> 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);