0

我这里有一些代码,它们已经在数组中包含数据(称为 setArray)。并将根据 transArray 的长度循环遍历该数据并将随机数据放入文本框字段。这是完整的代码:http: //jsfiddle.net/EABRJ/

var loadBills = function ()
{

clearFields();

mySetArray();  //Fills the transArray randomly with 1-4 items   

// TEMPORARY ALERT BELOW SO YOU CAN SEE THAT THE ARRAY IS BEING LOADED

//  alert(transArray);      

     for ( var i = 0; i <= transArray.length; i++)
    {
         var items = 'item' + i;
         var amounts = 'amount' + i;    
         splittedData = transArray[i].split(":");
         items.value += splittedData[0];
         amounts.value += splittedData[1];          
    }
}

var clearFields = function ()
{

    for ( i = 1; i <= 4; i++)
    {
     itemName = 'item' + i;
     $(itemName).value = "";
    }
    for ( i = 1; i <= 4; i++)
    {
     amountName = 'amount' + i;
     $(amountName).value = "";
    }
       $('total').value = "";   
   }

var mySetArray = function ()
{

 var myRandom = Math.floor((Math.random() * 100) / 25) + 1;  //a number between 1    and 4

 transArray = new Array();  //Resets the Array to empty

 if (myRandom == 1)
 {
    transArray[0] = "Food:200";
 }

 if (myRandom == 2)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
 }

 if (myRandom == 3)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
 }

 if (myRandom == 4)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
    transArray[3] = "Cable:130";
 }
}

    window.onload = function ()
    {
    $("loadbills").onclick = loadBills;
        $("clearfields").onclick = clearFields;
    }
4

1 回答 1

0

这是更正的JS代码..

演示小提琴

var loadBills = function () {

    clearFields();

    mySetArray(); //Fills the transArray randomly with 1-4 items    
    // TEMPORARY ALERT BELOW SO YOU CAN SEE THAT THE ARRAY IS BEING LOADED

    console.log(transArray);

    for (var i = 0; i <= transArray.length; i++) {
        if(transArray[i] != 'undefined' && transArray[i] != null) {
            var sdata = transArray[i].split(":");
            $("#item"+(i+1)).val(sdata[0]);
            $("#amount"+(i+1)).val(sdata[1]);
        }
    }
}

var clearFields = function () {

    for (i = 1; i <= 4; i++) {
        $("#item"+i).val("");
    }
    for (i = 1; i <= 4; i++) {
        $("#amount"+i).val("");
    }
    $('total').value = "";


}

var mySetArray = function () {

    var myRandom = Math.floor((Math.random() * 100) / 25) + 1; //a number between 1 and 4

    transArray = new Array(); //Resets the Array to empty

    if (myRandom == 1) {
        transArray[0] = "Food:200";
    }

    if (myRandom == 2) {
        transArray[0] = "Food:200";
        transArray[1] = "Toys:700";
    }

    if (myRandom == 3) {
        transArray[0] = "Food:200";
        transArray[1] = "Toys:700";
        transArray[2] = "Mortgage:1800";
    }

    if (myRandom == 4) {
        transArray[0] = "Food:200";
        transArray[1] = "Toys:700";
        transArray[2] = "Mortgage:1800";
        transArray[3] = "Cable:130";
    }
}


$("#loadbills").on("click", loadBills);
$("#clearfields").on("click", clearFields);
于 2013-10-20T06:33:25.277 回答