0

我有一个表单,允许用户选择 3 个日期期间(或 1 个或 2 个)。重复此模式,允许用户选择自定义范围的日期、每周、每月、每天等。如果有一种方法可以优化 jquery,这样我就不必为每个输入写出整个 jquery 函数?唯一真正改变的是 id 中的数字。

<input type='text' id='custom1from'><input type='text' id='custom1until'>
<input type='text' id='custom2from'><input type='text' id='custom2until'>
<input type='text' id='custom3from'><input type='text' id='custom3until'>

<input type='text' id='monthly1from'><input type='text' id='monthly1until'>
<input type='text' id='monthly2from'><input type='text' id='monthly2until'>
<input type='text' id='monthly3from'><input type='text' id='monthly3until'>



$("#custom1from").click(function() {
        $("#custom1from").datepicker();
      });

      $("#custom1until").click(function() {
        $("#custom1until").datepicker();
      });

      $("#custom2from").click(function() {
        $("#custom2from").datepicker();
      });

      $("#custom2until").click(function() {
        $("#custom2until").datepicker();
      });


  $("#custom3until").click(function() {
    $("#custom3until").datepicker();
  });
  $("#custom3until").click(function() {
    $("#custom3until").datepicker();
  });
4

2 回答 2

1
$('input').click(function(){
    $(this).datepicker(); // use "this" don't requery the DOM.
});

您最好将这些输入添加到一个类中,例如foo

$('input.foo').click(function(){
    $(this).datepicker();
});
于 2013-02-08T13:18:41.617 回答
1

只需给它一个类并选择它。

<input type='text' class="datepick" id='custom1from'><input type='text' id='custom1until'>
<input type='text' class="datepick" id='custom2from'><input type='text' id='custom2until'>
<input type='text' class="datepick" id='custom3from'><input type='text' id='custom3until'>

<input type='text' class="datepick" id='monthly1from'><input type='text' id='monthly1until'>
<input type='text' class="datepick" id='monthly2from'><input type='text' id='monthly2until'>
<input type='text' class="datepick" id='monthly3from'><input type='text' id='monthly3until'>

$(".datepick").datepicker();
于 2013-02-08T13:19:16.733 回答