如何随机为textbox onload分配值

时间:2013-08-29 08:54:00

标签: jquery arrays random onload

Onload页面值完全分配给数组中的文本框,但我需要从数组到文本框中随机分配值。请提出一些建议 代码链接

1 个答案:

答案 0 :(得分: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");
        }
    });
}

干杯