如何创建一个二维数组,其中维度由用户指定(在jQuery中)

时间:2021-10-11 15:41:58

I need to create a 2D array with dimension specified in input fields. And fill the generated fields so that it spirals clock-wise, starting from the bottom right corner.

我需要创建一个在输入字段中指定尺寸的2D数组。并填充生成的字段,使其从右下角开始顺时针旋转。

如何创建一个二维数组,其中维度由用户指定(在jQuery中)

I managed to create the 2D array but I don't know how to fill it. Any help is appreciated.

我设法创建了2D数组,但我不知道如何填充它。任何帮助表示赞赏。

$("button").click(function()
{
    var brr=parseInt($("#brr").val());
    var brs=parseInt($("#brs").val());
    for (i=1; i<=brs; i++)
    {
        for (j=1; j<=brr; j++)
        {
            $("#output").append("<div class='k'>o</div>");
        }
        $("#output").append("<br/>");
    }

})

2 个解决方案

#1


1  

something like this?

这样的事情?

http://jsfiddle.net/mig1098/d0beygLp

$("#button").click(function()              
{        
    $("#output").html('');
    var brs1=parseInt($("#brs1").val());
    var brs2=parseInt($("#brs2").val());
    var c=0;
    for (i=1; i<=brs1; i++){

     for (j=1; j<=brs2; j++){
        $("#output").append("<div class='k'>"+c+"</div>");
         c++;
     }
    $("#output").append("<div class=\"clearfix\"></div>");
    }

});

#2


0  

You can populate it this way: LIVE EXAMPLE HERE

你可以用这种方式填充它:这里的实例

 $("button").click(function() 
 {

    var brr=parseInt($("#brr").val());
    var brs=parseInt($("#brs").val());

    var counter = brr * brs;

    for (i=1; i<=brs; i++){

        for (j=1; j<=brr; j++)
        {

          $("#output").append("<div class='k'>" + (counter--) + "</div>");

        }

        $("#output").append("<br/>");

    }

});

#1


1  

something like this?

这样的事情?

http://jsfiddle.net/mig1098/d0beygLp

$("#button").click(function()              
{        
    $("#output").html('');
    var brs1=parseInt($("#brs1").val());
    var brs2=parseInt($("#brs2").val());
    var c=0;
    for (i=1; i<=brs1; i++){

     for (j=1; j<=brs2; j++){
        $("#output").append("<div class='k'>"+c+"</div>");
         c++;
     }
    $("#output").append("<div class=\"clearfix\"></div>");
    }

});

#2


0  

You can populate it this way: LIVE EXAMPLE HERE

你可以用这种方式填充它:这里的实例

 $("button").click(function() 
 {

    var brr=parseInt($("#brr").val());
    var brs=parseInt($("#brs").val());

    var counter = brr * brs;

    for (i=1; i<=brs; i++){

        for (j=1; j<=brr; j++)
        {

          $("#output").append("<div class='k'>" + (counter--) + "</div>");

        }

        $("#output").append("<br/>");

    }

});