嘿,伙计们,我需要一些代码来拆分一个数组,该数组包含一个字符串,该字符串是一个项目和数量,分隔符是 (:)。(例如 Gas:30 )将 transArray 中的元素加载到 item 和 amount 字段的 hmtl texboxes 的值中 请不要对评论过于苛刻,这是我的第一种语言类型语言。任何帮助表示赞赏!
var load = function ()
{
mySetArray(); //Fills the transArray randomly with 1-4 items
var item = '';
var amount = '';
for ( i=1; i<=transArray.length; i++)
{
item = 'item' + i;
amount = 'amount' + i;
transArray.split(":");
}
}
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 ()
{
$("load").onclick = load;
}