Onload 页面值完美地分配给数组中的文本框,但我需要将值从数组随机分配给文本框。请提出一些想法代码链接
user2717969
问问题
255 次
1 回答
0
不确定您是否想要什么,但这是一个命题:http: //jsfiddle.net/BGQN5/
步骤:1)选择n个输入2)选择n个随机日期3)随机日期4)将值分配给数组。
$(function(){
var i = prompt('Number of input ? ');
for(var j=0;j<i;j++)
{
add();
}
var test = new Array(i);
for(var j=0;j<i;j++)
{
test[j] = randomDate(new Date(2012, 0, 1), new Date());
}
test =shuffle(test);
for(var j=0;j<i;j++)
{
var d = test[j];
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
$("#datePicker"+(j+1)).val(curr_month+'/'+curr_date+'/'+curr_year);
}
console.log(test);
});
function shuffle(array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}
$("#datePicker0").kendoDatePicker({
close: function () {
console.log("closed datePicker");
}
});
function add() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
console.log();
cell1.innerHTML = '<input id="datePicker' + $(row).index() + '" /></td>';
initDatePicker($(row).index());
}
function initDatePicker(c) {
$('#datePicker' + c).kendoDatePicker({
close: function () {
console.log("closed datePicker");
}
});
}
干杯
于 2013-08-29T09:34:14.617 回答