简体   繁体   English

如果它大于或等于javascript中的某物,则想更改某物的值

[英]Want to change the value of something if its greater than or equal to something in javascript

Question is I have a couple of text fields in my rails app that are based off my user's anv date with this date I want to compare it with the date they pick for a my text box leave_start. 问题是我的Rails应用程序中有几个文本字段,这些文本字段基于用户的日期和该日期,我想将其与他们为我的文本框leaves_start选择的日期进行比较。 What I would like to have happen is if they choose a leave_start date that is greater than or equal to their anv date for this year change my future text box to say 'YES' if the leave start date is less than or equal to their anv change the future text box to say 'NO'. 我想发生的事情是,如果他们选择的休假开始日期大于或等于他们今年的anv日期,则如果休假开始日期小于或等于他们的anv,请将我的未来文本框更改为“ YES”将将来的文本框更改为“否”。 I've tried a couple different ways but each time no matter what date I pick it always changes future to 'NO' even if the date is greater than my user's anv. 我尝试了几种不同的方法,但是每次我选择的日期,即使日期大于用户的anv,也总是将其将来更改为“ NO”。 Any help would be greatly appreciated!!! 任何帮助将不胜感激!!!

*Side note this is my datepicker default format dateFormat: 'mm-dd-yy' for my leave_start text field. *旁注,这是我的datepicker默认格式dateFormat:'mm-dd-yy'用于我的Leave_start文本字段。

Here is my Application.js 这是我的Application.js

 $(document).ready(function(){
     $('#leave_start').change(function() {

        var anv = $('.anv').val();

        var l_start = $('#leave_start').val();

        if (l_start >= future) {
           $('#future').val('YES');
        } else if (l_start <= future ) {
           $('#future').val('NO');
        }
    });
 });

And here is my view 这是我的看法

 #this is the my var anv...


 %td.lt= f.text_field :grab_date, :id => 'anv', :class => 'anv', :value => @grab_adj.strftime('%m-%d-%Y')



 #this is my var l_start...

 %td.lt.feed= f.text_field :leave_start,  :label => false, :id => 'leave_start', :input_html => {:value => ''}



 #this is the future text box I want to change to 'YES' or 'NO'

 %td.lt.feed= f.input_field :future, :as => :select, :id => 'future', :class => 'future', :label => false, :collection => ["YES", "NO"]

First thing don't use 2-digit year format as discussed cause of popular bug Y2K Bug. 第一件事不要使用2位数的年格式,因为它是导致普遍存在的错误Y2K错误的原因。 Before comparing your dates you convert dates into milliseconds then compare. 在比较日期之前,请将日期转换为毫秒然后进行比较。

  $(document).ready(function(){
   $('#leave_start').change(function() {

   var anv_date = Date.parse($('.anv').val()); // Date in milliseconds
   var l_start_date = Date.parse($('#leave_start').val()); // Date in milliseconds

    if (l_start_date >= anv_date) {
       $('#future').val('YES');
    } else if (l_start_date <= anv_date ) {
       $('#future').val('NO');
    }

  }); });

The thing is you can't compare 2 date strings in javascript. 问题是您无法在javascript中比较2个日期字符串。 There are several approaches to do this kind of comparison. 有几种方法可以进行这种比较。 One of them is to use the Date object ( Compare two dates with JavaScript ). 其中之一是使用Date对象( 使用JavaScript比较两个日期 )。 Other alternative that i think is better, is to use a library like moment.js ( http://momentjs.co ) that makes manipulating dates a piece of cake. 我认为更好的另一种选择是使用诸如moment.jshttp://momentjs.co )之类的库,该库使操作日期变得moment.js

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

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