简体   繁体   English

Jquery获取最近类的未定义值

[英]Jquery getting undefined value of the closest class

I am trying to do validation comparing a pair of datetime values.我正在尝试比较一对日期时间值进行验证。 I created a dynamic form by which everytime user click on the "Add Session" button.我创建了一个动态表单,每次用户点击“添加会话”按钮时都会使用它。 A session form div will be appended.将附加会话表单 div。 For eg, I am trying to add 2 sessions and validate 2 pairs of session datetime values with the codes below.例如,我正在尝试添加 2 个会话并使用以下代码验证 2 对会话日期时间值。

    $('.startdatetime').each(function(){
        console.log($(this).val());
        console.log($(this).closest('.datetime-div').next().find('.enddatetime').val());
        if($(this).val() === "" || $(this).val() === null ){
            $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Start Date Time is required.<p>");
            hasErr = true;
        }else if(moment($(this).val()).isSameOrAfter($(this).closest('.datetime-div').next().find('.enddatetime').val(), 'minute')){ //validate start datetime < end datetime
            $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Invalid Date Time.<p>");
            hasErr = true;
        }
    });

I am getting undefined value for the first session end datetime in first loop which I am debugging using console.log.我在使用 console.log 调试的第一个循环中得到第一个会话结束日期时间的未定义值。

Codes below show my html snippet.下面的代码显示了我的 html 片段。

              <div class="row">
                <div class="datetime-div col-md-6 col-xs-12 col-sm-12">
                    <div class="form-group">
                        <div class="form-line startdatetime-input">
                            <label>Start Date Time</label>
                            <input type="text" class="datetimepicker form-control startdatetime" placeholder="Please choose date & time...">
                        </div>
                    </div>                    
                </div>
                <div class="datetime-div col-md-6 col-xs-12 col-sm-12">
                    <div class="form-group">
                        <div class="form-line enddatetime-input">
                            <label>End Date Time</label>
                            <input type="text" class="datetimepicker form-control enddatetime" placeholder="Please choose date & time...">
                        </div>
                    </div>                    
                </div>
            </div>

 $('.startdatetime').each(function() { console.log($(this).val()); console.log($(this).closest('.datetime-div').next().find('.enddatetime').val()); if ($(this).val() === "" || $(this).val() === null) { $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Start Date Time is required.<p>"); hasErr = true; } else if (moment($(this).val()).isSameOrAfter($(this).closest('.datetime-div').next().find('.enddatetime').val(), 'minute')) { //validate start datetime < end datetime $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Invalid Date Time.<p>"); hasErr = true; } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="datetime-div col-md-6 col-xs-12 col-sm-12"> <div class="form-group"> <div class="form-line startdatetime-input"> <label>Start Date Time</label> <input type="text" class="datetimepicker form-control startdatetime" placeholder="Please choose date & time..."> </div> </div> </div> <div class="datetime-div col-md-6 col-xs-12 col-sm-12"> <div class="form-group"> <div class="form-line enddatetime-input"> <label>End Date Time</label> <input type="text" class="datetimepicker form-control enddatetime" placeholder="Please choose date & time..."> </div> </div> </div> </div>

Any idea of getting the closest enddatetime value with the most appropriate jQuery selector?知道使用最合适的 jQuery 选择器获取最接近的 enddatetime 值吗?

Everything looks fine in your code.在您的代码中一切看起来都很好。 You are not getting an undefined , you are getting an empty value because both inputs are empty.您没有得到undefined ,而是得到一个空值,因为两个输入都是空的。 If you fill the attribute value , you will get those values printed in the console, run the code snippet I share and check.如果您填写属性value ,您将在控制台中打印这些值,运行我共享的代码片段并检查。 My changes are in the html input elements.我的更改在 html input元素中。 So it seems that you should be running the javascript you shared once the user has filled the inputs, not before like in the code snippet you shared.因此,似乎您应该在用户填写输入后运行您共享的 javascript,而不是像您共享的代码片段那样。

 $('.startdatetime').each(function() { console.log($(this).val()); console.log($(this).closest('.datetime-div').next().find('.enddatetime').val()); if ($(this).val() === "" || $(this).val() === null) { $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Start Date Time is required.<p>"); hasErr = true; } else if (moment($(this).val()).isSameOrAfter($(this).closest('.datetime-div').next().find('.enddatetime').val(), 'minute')) { //validate start datetime < end datetime $(this).closest('.startdatetime-input').after("<p class='error-message font-bold col-pink'>Invalid Date Time.<p>"); hasErr = true; } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="datetime-div col-md-6 col-xs-12 col-sm-12"> <div class="form-group"> <div class="form-line startdatetime-input"> <label>Start Date Time</label> <input type="text" value="valueForStartDateTime" class="datetimepicker form-control startdatetime" placeholder="Please choose date & time..."> </div> </div> </div> <div class="datetime-div col-md-6 col-xs-12 col-sm-12"> <div class="form-group"> <div class="form-line enddatetime-input"> <label>End Date Time</label> <input type="text" value="valueForEndDateTime" class="datetimepicker form-control enddatetime" placeholder="Please choose date & time..."> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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