检测2个日历值何时更改

时间:2016-03-14 15:06:46

标签: javascript jquery ajax

我正在制作财务报告,用户可以选择2个日期search_date1search_date2,然后生成月度报告。

我首先创建了一个只有一个日历的日报,当它被更改时,我会将一些AJAX脚本应用到它并且它可以正常工作:

var myApp = {};
myApp.search_date = "";
 document.getElementById('search_date').onchange = function (e) {
    if (this.value != myApp.search_date) {
        var d = $("#search_date").val();
    $.ajax({
        ...
    });
  }
}

现在我无法知道如何检测这两个日历是否根据其值更改为应用AJAX脚本。

修改

执行以下操作是否正确:

var myApp = {};
myApp.search_date1 = "";
myApp.search_date2 = "";
 document.getElementById('search_date1').onchange = function (e) {
    if (this.value != myApp.search_date1) {
        var d1 = $("#search_date1").val();
        document.getElementById('search_date2').onchange = function (e) {
             if (this.value != myApp.search_date2) {
                     var d2 = $("#search_date2").val();
                     $.ajax({
                        ...
                     })
             }
         });
     }
});

4 个答案:

答案 0 :(得分:1)

您在编辑中所做的工作可能会奏效,但这样做会更好(也更容易)

var myApp = {};
myApp.original_search_date1 = $("#search_date1").val();
myApp.original_search_date2 = $("#search_date2").val();
myApp.search_date1 = $("#search_date1").val();
myApp.search_date2 = $("#search_date2").val();

document.getElementById('search_date1').onchange = function (e) {
    if ($("#search_date1").val() != myApp.search_date1) {
        myApp.search_date1 = $("#search_date1").val();
        sendAjax();
    }
});

document.getElementById('search_date2').onchange = function (e) {
    if ($("#search_date2").val() != myApp.search_date2) {
        myApp.search_date2 = $("#search_date2").val();
        sendAjax();
     }
});

function sendAjax() {
    if (myApp.original_search_date1 !== myApp.search_date1 &&
        myApp.original_search_date2 !== myApp.search_date2) {
        $.ajax({
            ...
        });
    }
}

答案 1 :(得分:1)

试试这个:



<add name="Access-Control-Allow-Headers" 
     value="Origin, Content-Type, Accept, Authorization" 
     />
&#13;
var temp = {
  from: null,
  to: null
}

document.getElementById('from').onchange = function(e){
  
 temp.from = e.target.value;
  goAjax();
}
document.getElementById('to').onchange = function(e){
 temp.to = e.target.value;
  goAjax();
}

function goAjax(){
  if(temp.from && temp.to && new Date(temp.from) < new Date(temp.to)){
    //do ajax call
    console.log('valid')
  }
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我会捕获两个元素的更改事件:

$("#search_date1, #search_date2").on('change',function(){
    var d1 = $("#search_date1").val();
    var d2 = $("#search_date2").val();
    $.ajax({...});
});

答案 3 :(得分:1)

你不能设置一个变量来检查它是否已经用true / false更改,然后在两个变量都为真的情况下运行脚本。

类似的东西,

     var searchOneToggled = false, 
         searchTwoToggled = false;

     $('#search_date_one').on('input', function() {
         searchOneToggled = true;
         runYourFunction();
     });

     $('#search_date_two').on('input', function() {
         searchTwoToggled = true;
         runYourFunction();
     });

     function runYourFunction() {
         if(searchOneToggled === true && searchTwoToggled === true) {
            alert('hello world');
         }
     }
相关问题