首页 \ 问答 \ Javascript / jQuery - 如何调用switch case从另一个函数执行(Javascript/jQuery - how to call a switch case to execute from another function)

Javascript / jQuery - 如何调用switch case从另一个函数执行(Javascript/jQuery - how to call a switch case to execute from another function)

我想知道是否可以从另一个功能调用一个开关盒。 在我的页面上,用户可以使用元素$("#timeInterval")选择预定义的时间间隔,其中一个选项是从datepicker中选择日期,默认情况下不可见。 根据这些日期,我正在创建图表。 由于我在select元素上也有几个图表,我需要确保它们获得正确的日期,无论是表单选择选项还是日期选择器。 我现在的问题是我需要检查日期选择器是否可见我可以从中获取日期,如果没有,我应该获得所选选项并执行具有该情况的日期的函数。

这是我的代码:

$("#timeInterval").change(function() {

    var date = moment().format("YYYY-MM-DD");
    var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
    var tomorrow = moment().add(1, 'day').format('YYYY-MM-DD');
    var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
    var thirtyDaysAgo = moment().subtract(29, 'day').format('YYYY-MM-DD');
    var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
    var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
    var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
    var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
    var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
    var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');

    switch($("#timeInterval").val()) {
      case "allTime":
        $(".datePickers").hide();
        timelines(0, 0);
        barCharts(0, 0);
        $( "#list" ).empty();
        mostArticles(0, 0);
        break;
      case "today":
        $(".datePickers").hide();
        timelines(date, tomorrow);
        barCharts(date, tomorrow);
        $( "#list" ).empty();
        mostArticles(date, tomorrow);
        break;
      case "yesterday":
        $(".datePickers").hide();
        timelines(yesterday, date);
        barCharts(yesterday, date);
        $( "#list" ).empty();
        mostArticles(yesterday, date);
        break;
      case "lastSevenDays":
        $(".datePickers").hide();
        timelines(sevenDaysAgo, date);
        barCharts(sevenDaysAgo, date);
        $( "#list" ).empty();
        mostArticles(sevenDaysAgo, date);
        break;
      case "lastThirtyDays":
        $(".datePickers").hide();
        timelines(thirtyDaysAgo, tomorrow);
        barCharts(thirtyDaysAgo, tomorrow);
        $( "#list" ).empty();
        mostArticles(thirtyDaysAgo, tomorrow);
        break;
      case "thisWeek":
        $(".datePickers").hide();
        timelines(thisWeekStart, tomorrow);
        barCharts(thisWeekStart, tomorrow);
        $( "#list" ).empty();
        mostArticles(thisWeekStart, tomorrow);
        break;
      case "lastWeek":
        $(".datePickers").hide();
        timelines(lastWeekStart, lastWeekEnd);
        barCharts(lastWeekStart, lastWeekEnd);
        $( "#list" ).empty();
        mostArticles(lastWeekStart, lastWeekEnd);
        break;
      case "thisMonth":
        $(".datePickers").hide();
        timelines(startOfMonth, date);
        barCharts(startOfMonth, date);
        $( "#list" ).empty();
        mostArticles(startOfMonth, date);
        break;
      case "lastMonth":
        $(".datePickers").hide();
        timelines(startOfLastMonth, endOfLastMonth);
        barCharts(startOfLastMonth, endOfLastMonth);
        $( "#list" ).empty();
        mostArticles(startOfLastMonth, endOfLastMonth);
        break;
      default:
        $(".datePickers").show();
      }
  });

$('#datePicker').fdatepicker({
        closeButton: false,
    initialDate: weekBack,
        format: 'dd.mm.yyyy',
    endDate: dayBack,
    }).on('changeDate', function (ev) {
      var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
      var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
        timelines(dateFrom, dateTo);
      barCharts(dateFrom, dateTo);
      $( "#list" ).empty();
      mostArticles(dateFrom, dateTo);
  });

  $('#datePicker1').fdatepicker({
        closeButton: false,
    initialDate: date,
        format: 'dd.mm.yyyy',
    endDate: date,
    }).on('changeDate', function (ev) {
    var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
    var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    timelines(dateFrom, dateTo);
    barCharts(dateFrom, dateTo);
    $( "#list" ).empty();
    mostArticles(dateFrom, dateTo);
  });

  //timeline charts
  $("#timelines").on("change", function() {
    if ($("#datePicker").is(":visible")){
      var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
      var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    }
    else{
      console.log($( "#timeInterval" ).val());

    }
    timelines(dateFrom, dateTo);
  }).trigger("change");

I am wondering if it is possible to call a switch case from another function. On my page user has a possibility to select predefined time intervals with element $("#timeInterval") and one of the options is to choose dates from datepicker, which is not visible by default. Based on those dates I am creating charts. Since I have few charts that are also available on select element, I need to make sure they get the correct dates, either form select option, or datepicker. My problem now is that I need to check if the datepicker is visible I can get dates from it, if not, I should get the selected option and execute functions with dates for that case.

This is my code:

$("#timeInterval").change(function() {

    var date = moment().format("YYYY-MM-DD");
    var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
    var tomorrow = moment().add(1, 'day').format('YYYY-MM-DD');
    var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
    var thirtyDaysAgo = moment().subtract(29, 'day').format('YYYY-MM-DD');
    var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
    var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
    var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
    var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
    var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
    var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');

    switch($("#timeInterval").val()) {
      case "allTime":
        $(".datePickers").hide();
        timelines(0, 0);
        barCharts(0, 0);
        $( "#list" ).empty();
        mostArticles(0, 0);
        break;
      case "today":
        $(".datePickers").hide();
        timelines(date, tomorrow);
        barCharts(date, tomorrow);
        $( "#list" ).empty();
        mostArticles(date, tomorrow);
        break;
      case "yesterday":
        $(".datePickers").hide();
        timelines(yesterday, date);
        barCharts(yesterday, date);
        $( "#list" ).empty();
        mostArticles(yesterday, date);
        break;
      case "lastSevenDays":
        $(".datePickers").hide();
        timelines(sevenDaysAgo, date);
        barCharts(sevenDaysAgo, date);
        $( "#list" ).empty();
        mostArticles(sevenDaysAgo, date);
        break;
      case "lastThirtyDays":
        $(".datePickers").hide();
        timelines(thirtyDaysAgo, tomorrow);
        barCharts(thirtyDaysAgo, tomorrow);
        $( "#list" ).empty();
        mostArticles(thirtyDaysAgo, tomorrow);
        break;
      case "thisWeek":
        $(".datePickers").hide();
        timelines(thisWeekStart, tomorrow);
        barCharts(thisWeekStart, tomorrow);
        $( "#list" ).empty();
        mostArticles(thisWeekStart, tomorrow);
        break;
      case "lastWeek":
        $(".datePickers").hide();
        timelines(lastWeekStart, lastWeekEnd);
        barCharts(lastWeekStart, lastWeekEnd);
        $( "#list" ).empty();
        mostArticles(lastWeekStart, lastWeekEnd);
        break;
      case "thisMonth":
        $(".datePickers").hide();
        timelines(startOfMonth, date);
        barCharts(startOfMonth, date);
        $( "#list" ).empty();
        mostArticles(startOfMonth, date);
        break;
      case "lastMonth":
        $(".datePickers").hide();
        timelines(startOfLastMonth, endOfLastMonth);
        barCharts(startOfLastMonth, endOfLastMonth);
        $( "#list" ).empty();
        mostArticles(startOfLastMonth, endOfLastMonth);
        break;
      default:
        $(".datePickers").show();
      }
  });

$('#datePicker').fdatepicker({
        closeButton: false,
    initialDate: weekBack,
        format: 'dd.mm.yyyy',
    endDate: dayBack,
    }).on('changeDate', function (ev) {
      var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
      var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
        timelines(dateFrom, dateTo);
      barCharts(dateFrom, dateTo);
      $( "#list" ).empty();
      mostArticles(dateFrom, dateTo);
  });

  $('#datePicker1').fdatepicker({
        closeButton: false,
    initialDate: date,
        format: 'dd.mm.yyyy',
    endDate: date,
    }).on('changeDate', function (ev) {
    var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
    var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    timelines(dateFrom, dateTo);
    barCharts(dateFrom, dateTo);
    $( "#list" ).empty();
    mostArticles(dateFrom, dateTo);
  });

  //timeline charts
  $("#timelines").on("change", function() {
    if ($("#datePicker").is(":visible")){
      var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
      var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    }
    else{
      console.log($( "#timeInterval" ).val());

    }
    timelines(dateFrom, dateTo);
  }).trigger("change");

原文:https://stackoverflow.com/questions/37572478
更新时间:2019-12-13 07:38

最满意答案

是的可能。 把你的开关放在一个功能; 称之为DoTimeIntervalWork之类的东西。 在该功能内,您可以做您需要做的事情。 确保传入或以其他方式访问$("#timeInterval").val() ,以使其正常工作。 重要的是你打电话到timelines(yesterday, date);barCharts(yesterday, date); 函数内部还有其他函数调用。 如果不是,您可以让交换机返回一个函数对象,然后由DoTimeIntervalWork的调用者调用。

$("#timeInterval").change(function() {

var date = moment().format("YYYY-MM-DD");
var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
var tomorrow = moment().add(1, 'day').format('YYYY-MM-DD');
var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
var thirtyDaysAgo = moment().subtract(29, 'day').format('YYYY-MM-DD');
var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');

DoTimeIntervalWork($("#timeInterval").val())
  });

$('#datePicker').fdatepicker({
    closeButton: false,
initialDate: weekBack,
    format: 'dd.mm.yyyy',
endDate: dayBack,
}).on('changeDate', function (ev) {
  var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
  var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    timelines(dateFrom, dateTo);
  barCharts(dateFrom, dateTo);
  $( "#list" ).empty();
  mostArticles(dateFrom, dateTo);
});

  $('#datePicker1').fdatepicker({
    closeButton: false,
initialDate: date,
    format: 'dd.mm.yyyy',
endDate: date,
}).on('changeDate', function (ev) {
var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
timelines(dateFrom, dateTo);
barCharts(dateFrom, dateTo);
$( "#list" ).empty();
mostArticles(dateFrom, dateTo);
  });

//timeline charts
$("#timelines").on("change", function() {
 if ($("#datePicker").is(":visible")){
   var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
   var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
 }
 else{
     console.log($( "#timeInterval" ).val());

   }
   timelines(dateFrom, dateTo);
  }).trigger("change");

function DoTimeIntervalWork(timeInterval){
 switch(timeInterval) {
  case "allTime":
    $(".datePickers").hide();
    timelines(0, 0);
    barCharts(0, 0);
    $( "#list" ).empty();
    mostArticles(0, 0);
    break;
  case "today":
    $(".datePickers").hide();
    timelines(date, tomorrow);
    barCharts(date, tomorrow);
    $( "#list" ).empty();
    mostArticles(date, tomorrow);
    break;
  case "yesterday":
    $(".datePickers").hide();
    timelines(yesterday, date);
    barCharts(yesterday, date);
    $( "#list" ).empty();
    mostArticles(yesterday, date);
    break;
  case "lastSevenDays":
    $(".datePickers").hide();
    timelines(sevenDaysAgo, date);
    barCharts(sevenDaysAgo, date);
    $( "#list" ).empty();
    mostArticles(sevenDaysAgo, date);
    break;
  case "lastThirtyDays":
    $(".datePickers").hide();
    timelines(thirtyDaysAgo, tomorrow);
    barCharts(thirtyDaysAgo, tomorrow);
    $( "#list" ).empty();
    mostArticles(thirtyDaysAgo, tomorrow);
    break;
  case "thisWeek":
    $(".datePickers").hide();
    timelines(thisWeekStart, tomorrow);
    barCharts(thisWeekStart, tomorrow);
    $( "#list" ).empty();
    mostArticles(thisWeekStart, tomorrow);
    break;
  case "lastWeek":
    $(".datePickers").hide();
    timelines(lastWeekStart, lastWeekEnd);
    barCharts(lastWeekStart, lastWeekEnd);
    $( "#list" ).empty();
    mostArticles(lastWeekStart, lastWeekEnd);
    break;
  case "thisMonth":
    $(".datePickers").hide();
    timelines(startOfMonth, date);
    barCharts(startOfMonth, date);
    $( "#list" ).empty();
    mostArticles(startOfMonth, date);
    break;
  case "lastMonth":
    $(".datePickers").hide();
    timelines(startOfLastMonth, endOfLastMonth);
    barCharts(startOfLastMonth, endOfLastMonth);
    $( "#list" ).empty();
    mostArticles(startOfLastMonth, endOfLastMonth);
    break;
  default:
    $(".datePickers").show();
  }
}

Yes its possible. Put your switch in a function; call it something like DoTimeIntervalWork or something. Inside that function you can do what you need to do. Make sure you pass in or otherwise access the value of $("#timeInterval").val() in order to make it work properly. All that matters is that your calls to timelines(yesterday, date); and barCharts(yesterday, date); and other function calls are available inside the function. If they're not, you can have your switch return a function object that is then called by the caller of DoTimeIntervalWork.

$("#timeInterval").change(function() {

var date = moment().format("YYYY-MM-DD");
var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
var tomorrow = moment().add(1, 'day').format('YYYY-MM-DD');
var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
var thirtyDaysAgo = moment().subtract(29, 'day').format('YYYY-MM-DD');
var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');

DoTimeIntervalWork($("#timeInterval").val())
  });

$('#datePicker').fdatepicker({
    closeButton: false,
initialDate: weekBack,
    format: 'dd.mm.yyyy',
endDate: dayBack,
}).on('changeDate', function (ev) {
  var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
  var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
    timelines(dateFrom, dateTo);
  barCharts(dateFrom, dateTo);
  $( "#list" ).empty();
  mostArticles(dateFrom, dateTo);
});

  $('#datePicker1').fdatepicker({
    closeButton: false,
initialDate: date,
    format: 'dd.mm.yyyy',
endDate: date,
}).on('changeDate', function (ev) {
var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
timelines(dateFrom, dateTo);
barCharts(dateFrom, dateTo);
$( "#list" ).empty();
mostArticles(dateFrom, dateTo);
  });

//timeline charts
$("#timelines").on("change", function() {
 if ($("#datePicker").is(":visible")){
   var dateFrom = $("#datePicker").val().split(".").reverse().join("-");
   var dateTo = $("#datePicker1").val().split(".").reverse().join("-");
 }
 else{
     console.log($( "#timeInterval" ).val());

   }
   timelines(dateFrom, dateTo);
  }).trigger("change");

function DoTimeIntervalWork(timeInterval){
 switch(timeInterval) {
  case "allTime":
    $(".datePickers").hide();
    timelines(0, 0);
    barCharts(0, 0);
    $( "#list" ).empty();
    mostArticles(0, 0);
    break;
  case "today":
    $(".datePickers").hide();
    timelines(date, tomorrow);
    barCharts(date, tomorrow);
    $( "#list" ).empty();
    mostArticles(date, tomorrow);
    break;
  case "yesterday":
    $(".datePickers").hide();
    timelines(yesterday, date);
    barCharts(yesterday, date);
    $( "#list" ).empty();
    mostArticles(yesterday, date);
    break;
  case "lastSevenDays":
    $(".datePickers").hide();
    timelines(sevenDaysAgo, date);
    barCharts(sevenDaysAgo, date);
    $( "#list" ).empty();
    mostArticles(sevenDaysAgo, date);
    break;
  case "lastThirtyDays":
    $(".datePickers").hide();
    timelines(thirtyDaysAgo, tomorrow);
    barCharts(thirtyDaysAgo, tomorrow);
    $( "#list" ).empty();
    mostArticles(thirtyDaysAgo, tomorrow);
    break;
  case "thisWeek":
    $(".datePickers").hide();
    timelines(thisWeekStart, tomorrow);
    barCharts(thisWeekStart, tomorrow);
    $( "#list" ).empty();
    mostArticles(thisWeekStart, tomorrow);
    break;
  case "lastWeek":
    $(".datePickers").hide();
    timelines(lastWeekStart, lastWeekEnd);
    barCharts(lastWeekStart, lastWeekEnd);
    $( "#list" ).empty();
    mostArticles(lastWeekStart, lastWeekEnd);
    break;
  case "thisMonth":
    $(".datePickers").hide();
    timelines(startOfMonth, date);
    barCharts(startOfMonth, date);
    $( "#list" ).empty();
    mostArticles(startOfMonth, date);
    break;
  case "lastMonth":
    $(".datePickers").hide();
    timelines(startOfLastMonth, endOfLastMonth);
    barCharts(startOfLastMonth, endOfLastMonth);
    $( "#list" ).empty();
    mostArticles(startOfLastMonth, endOfLastMonth);
    break;
  default:
    $(".datePickers").show();
  }
}
2016-06-01

相关问答

更多

如何减少android SQLite中的自动增量_id?(How to decrease the Auto increment _id in android SQLite?)

编辑:也许我应该说清楚只是插入具有正确id的行而不是操纵序列号肯定比下面的方法更好。 如果表中没有id = 3的行,则即使在AUTOINCREMENT表中也可以在id中插入固定值。 也就是说,如果您确定 ,可以使用自动增量值设置为任意值; UPDATE sqlite_sequence set seq=<next sequence no -1> where name=<table name>; 也就是说,如果你想在表'TableA'上的下一个插入中使用AUTOINCREMENT来生成5,那么你可

脚本拼图

最新问答

更多
  • NumPy标量,广播和__iadd__方法(NumPy scalars, broadcasting and the __iadd__ method)
  • 快速排序算法输出错误(Getting wrong output for quicksort algorithm)
  • Rails管理员:没有Rails 4应用程序的编辑,显示或删除按钮(Rails Admin: No edit, show, or delete buttons for Rails 4 app)
  • 部分刷新问题触发两次(Issue with partial refresh triggered twice)
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • EF6.1 beta 1如何获得Tooling Consolidation功能?(EF6.1 beta 1 how to get the Tooling Consolidation feature?)
  • 适用于Windows和PHP 5.5的Phalcon(Phalcon for Windows and PHP 5.5)
  • 有没有办法忽略hg责任中的提交?(Is there a way to ignore a commit in hg blame?)
  • Desire2通过API学习访问学期的权限(Desire2Learn Permissions to Access Semesters Via API)
  • 从aspx将服务器的文件复制到另一台服务器(Copy a file of a server to another server from aspx)
  • 使用YSlow / Web Developer扩展程序确定页面大小(Figuring out page size with YSlow / Web Developer extension)
  • Select语句将字符串中的前7个字符与其他表中的其他7个字符进行比较(Select statement which compares first 7 chars in a string with other 7 chars in other table)
  • 检测对注册表的更改,防止更改等(Detect changes to registry, prevent changes, etc)
  • 用户的鼠标/键盘运动分析是否有特殊名称?(Does the analysis of mouse/keyboard motions of a user have a special name?)
  • 获取Rails中资源的路由列表(Get list of routes for a resource in Rails)
  • 用函数值除以数字(Divide a function value by a number)
  • 熊猫Python:如何从今天开始创建失效列?(Pandas Python: How to create lapse since today column?)
  • GDK Bug报告(GDK Bug Reports)
  • 困惑于在python中循环遍历列表(confused about looping through lists in python)
  • 'less'由'which'输出指定的文件('less' the file specified by the output of 'which')
  • 如何在ASP.NET中优化AJAX(How to optimize AJAX in ASP.NET)
  • 在文本文件中转换纪元时间(Convert epoch times in text file)
  • 表细胞伸展(Table cell stretching)
  • GridView控件(GridView Control)
  • 从构建定义文件控制pdb文件输出(Control pdb file output from build defintion file)
  • *将方法传递给类(*Passing a Method into a Class)
  • 如何在HttpReceiveHttpRequest()中使用HTTPS?(How to use HTTPS with HttpReceiveHttpRequest()?)