我有一个允许用户选择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();
});
答案 0 :(得分:1)
$('input').click(function(){
$(this).datepicker(); // use "this" don't requery the DOM.
});
您最好将这些输入添加到类中,例如foo
:
$('input.foo').click(function(){
$(this).datepicker();
});
答案 1 :(得分: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();