简体   繁体   English

datepicker第一次加载不起作用,jQuery

[英]datepicker not working on first load, jquery

I am working with datepicker jquery for 2 input "from" and "to" filter. 我正在使用datepicker jQuery为2个输入“ from”和“ to”过滤器。 I want to make it required if one of the filters (from/to) has been selected. 如果选择了一个过滤器(从/到),我想将其设为必需。 And if the user doesn't want to use the date filter, I want to remove the required attribute. 并且,如果用户不想使用日期过滤器,我想删除必需的属性。

However, after I select a date in my "from" input, the action is not affected on first submit. 但是,在“来自”输入中选择一个日期后,该操作在首次提交时不会受到影响。 Somehow the "from" input didn't recognize value from datepicker on the first load. 某种程度上,“首次”输入中的“来自”输入无法识别日期选择器的值。

Anyone know why it can't work on the first submit? 有人知道为什么它不能在第一次提交时起作用吗?

here is the code for jquery: 这是jquery的代码:

$(document).ready(function () {
    if ($('#to').val().length == 0 && $('#from').val().length == 0) {
        $('#from').removeAttr('required');
        $('#to').removeAttr('required');
    }
    else if ($('#to').val().length > 0 ) {
        $('#from').attr('required');
    }
    else if ($('#from').val().length > 0) {
        $('#to').attr('required');
    }
});

for the input from and to, I set it required by default. 对于往返的输入,我默认将其设置为必填。

 @Html.TextBoxFor(modelitem => Model.startdate, new { @class = "datepicker form-control" ,@placeholder = "Select From Date" , @id="from" , @required = "required" })

 @Html.TextBoxFor(modelitem => Model.finishdate, new { @class = "datepicker form-control", @placeholder = "Select To Date" , @id = "to" , @required = "required" })

Please let me know the problem! 请让我知道问题所在!

try to remove it inside the document .ready function like 尝试将其删除document .ready函数,例如

<script>
     if ($('#to').val().length == 0 && $('#from').val().length == 0) {
        $('#from').removeAttr('required');
        $('#to').removeAttr('required');
    }
    else if ($('#to').val().length > 0 ) {
        $('#from').attr('required');
    }
    else if ($('#from').val().length > 0) {
        $('#to').attr('required');
    }
</script>

or use: 或使用:

Alert("this is want to know on first run")

inside your if else statement 在您的if else语句中

Addition to: Right click the page = > console menu on first run and check some errors or any jquery is running or not. 另外:第一次运行时,右键单击页面=>控制台菜单,并检查一些错误或任何jquery是否正在运行。

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

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