繁体   English   中英

日期选择器字段中的表单验证无法正常工作

[英]Form Validation in Date-Picker Field Not Working Properly

我有以下两个字段用于Date-Picker。

鉴于:

<div class="form-line">                                
     <input type="text" id="iCaseFileDate" name="CaseFileDate" placeholder="Case File Date*" class="datepicker form-control" required/>                                
</div>

<div class="form-line">
     <input type="text" id="iHearingDate" name="HearingDate" placeholder="Hearing Date*" class="datepicker form-control" required/>                                
</div>

当我单击Submit按钮时,它会很好地呈现带有'required'属性的'inputForm'的所有字段,就像每当我将这些字段保留为空然后单击Submit时'required'属性都能正常工作一样。 但是在那之后,如果我从“日期选择器”字段中选择一个日期,则不会删除“案件文件日期和听证日期”字段的“此字段是必需的”。

JavaScript代码:

$(document).ready(function () {

    $('#submit_button').on('click', function () {
        $('#inputForm').valid()
    });
});

    $('#iCaseFileDate').on('change', function () {
        if ($('#iCaseFileDate').val()) {
            $('#iCaseFileDate').removeAttr("required");
        }
    });        

    $('#iHearingDate').on('change', function () {
        if ($('#iHearingDate').val()) {
            $('#iHearingDate').removeAttr("required");
        }
    });

提交按钮代码:

<div class="modal-footer">
     <button type="submit" id="submit_button" class="btn btn-success waves-light" onclick="saveData()">SAVE</button>
     <button type="button" class="btn btn-warning waves-red" data-dismiss="modal">Close</button>
</div>

function saveData() {
            $("#inputForm").submit();
        }
        $("#inputForm").on("submit", function (event) {
            event.preventDefault();
            tinyMCE.triggerSave();
            var $this = $(this);

            var frmValues = $this.serialize();
            var isValid = $("#inputForm").valid();
            if (isValid == false) {

            }
            else {
                $.ajax({
                    type: 'POST',
                    url: '/ClientInfo/Save',
                    data: frmValues
                })
                    .done(function (result) {
                        if (result) {
                            alert(result.info);
                            clearInputFields();
                            $('#inputModal').modal('hide');
                            ReloadTable();
                        }
                    })
                    .fail(function (xhr) {
                        alert("error");
                    });
            }

        });

[添加图像以更好地进行说明]

[ 在填写任何输入字段并单击提交按钮之前 ] [ 在此处输入图片说明 ] 1

[ 填写输入字段值后,“案例文件日期和听证日期所需的消息不会删除 ] [ 在此处输入图片说明 ] 2

请帮我解决这个问题。 我只想在这些字段为空时显示“此字段为必填”消息,并在这些字段的日期选择器中选择了值时隐藏此消息。

从您的问题还不清楚,您是否在MVC Razor视图中使用模型绑定?

我想你可以使用下面的jQuery代码

$('#iCaseFileDate').on('change', function () {
    if ($('#iCaseFileDate').val().length>0) {
        $('#iCaseFileDate').removeAttr("required");
//comment
//Find the div containing validation message * the field is required* and remove it 
//like below
    $(this).next('.your_validation_message_div').remove(); 

    }
});        

$('#iHearingDate').on('change', function () {
    if ($('#iHearingDate').val().length>0) {
        $('#iHearingDate').removeAttr("required");
 //comment
//Find the div containing validation message * the field is required* and remove it 
//like below
    $(this).next('.your_validation_message_div').remove(); 
    }
});

暂无
暂无

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

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