jQuery基于muipicker实现仿ios时间选择

前端之家收集整理的这篇文章主要介绍了jQuery基于muipicker实现仿ios时间选择前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先我们先来看原始的muipicker的例子

<Meta charset="utf-8">


原生 SELECT(选择框)在不同的设备上UI可能会有差异,并且不支持多级联动(多个 SELECT 可实现,但较麻烦),故mui封装了picker组件,参见如下示例。

在此基础上修改为类似ios选择时间的插件。

把里面数据换成下面的数据就可以了。

// 年月
var yearArray = new Array();
for (var i = 0; i < 10; i ++) {
var monthArray = new Array();
for (var j = 0; j < 12; j ++) {
var month = new Object();
month.value = j + 1 + "月";
month.text = j + 1 + "月";
monthArray.push(month);
}
var year = new Object();
year.value = i + 2016 + "年" ;
year.text = i + 2016 + "年";
year.children = monthArray;
yearArray.push(year);
};

//年月日
var yearArray = new Array();
for (var i = 0; i < 10; i ++) {
var monthArray = new Array();
for (var j = 0; j < 12; j ++) {
//月的最后一天
var year = i + 2016;
var month = j;
var lastDay = new Date(year,month + 1,0).getDate();//month 要加1,原本month是从0开始的,但是因为0是这个月的上个月,所以到了上个月.
if(year == 2016 && month == 1) {
console.log("lastday" + lastDay);
}
var dayArray = new Array();
for(var k = 0; k < lastDay; k ++) {
var day = new Object();
day.value = k + 1 + "日";
day.text = k + 1 + "日";
dayArray.push(day);
}
var month = new Object();
month.children = dayArray;
month.value = j + 1 + "月";
month.text = j + 1 + "月";
monthArray.push(month);
}
var year = new Object();
year.value = i + 2016 + "年" ;
year.text = i + 2016 + "年";
year.children = monthArray;
yearArray.push(year);
};
console.log(yearArray);

// 小时分钟
var hoursec = new Array();
for (var i = 0; i < 24; i ++) {
var hsChildrenArray = new Array();
for (var j = 0; j < 60; j ++) {

      var childrenObject = new Object();
      var secTrue = j ;
      if(j < 10) {
        var secTrue = j ;
        childrenObject.value = "0" + secTrue;
        childrenObject.text = "0" + secTrue;
      }else {
        childrenObject.value = secTrue ;
        childrenObject.text = secTrue;
      }

      hsChildrenArray.push(childrenObject);
    }

    var object = new Object();
    if(i < 10) {
      object.value = "0" + i + ":" ;
      object.text = "0" + i + ":";
    }else {
      object.value = i +":" ;
      object.text = i + ":";
    }
    object.children = hsChildrenArray;
    hoursec.push(object);
  };
});

})(mui,document);

效果图:

年月日

小时分钟

效果只有在手机端或者手机chrome的手机模拟器中可以看到。

最后附上muipicker的github地址

猜你在找的jQuery相关文章