使用javascript创建多维数组(nXn)矩阵 [英] creating a multidimensional array (nXn) matrix using javascript

查看:112
本文介绍了使用javascript创建多维数组(nXn)矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个矩阵表。插入矩阵的值将是来自html表的用户输入(基本上是一个唯一的配对表,它一次配对两个元素,每个用户可以选择他喜欢哪个,以及在一个比例上优先选择多少1-9使用单选按钮)。它是首选项,即1-9插入矩阵表和确定矩阵长度的唯一配对数。



问题现在我想要将点击的无线电的值放入matix中。我知道如何得到它,但只是不知道如何将其融入矩阵。这是我现在的代码,如果需要,请向我提问。谢谢:



 $(function(){
var tableLenght = new Array();
$(' #matrix')。click(function(){
var counter = 0 ;
$( #riskForm tr)。each(function(){
// 忽略标题表格(标题)
if (counter > = < span class =code-digit> 1 ){
tableLenght.push($( this )。nearest(' tr')。children( TD:当量(1))文本());
}
counter ++;
});

// 获取表格中的唯一属性
< span class =code-keyword> var
uniqueList = new Array();
// 将第一个元素推送到列表
uniqueList.push(tableLenght [< span class =code-digit> 0 ]); // 将当前元素推送到数组
var cur = tableLenght [ 0 ]; // 默认设置第一个元素
for var i = 1 ; i < ; tableLenght.length; i ++){ // 遍历我们数组的整个长度
if (cur!= tableLenght [i]){ // 检查当前是否与下一个相同
cur = tableLenght [i]; // 设置新元素
uniqueList.push(tableLenght [i]); // 推送到数组
}
}
alert( uniqueList); // 仅警告tableLenght数组中的唯一表行
var myArray = new 数组(uniqueList);

for var i = 0 ; i < uniqueList; i ++){
myArray [i] = new Array(uniqueList);
for var j = 0 ; j < uniqueList; j ++){
myArray [i] [j] = ' ';
}
}
});

// 显示/获取所选单选按钮的值
function showValues(){
var fields = $( :输入)serializeArray();
$( #results)。empty();
jQuery.each(fields,function(i,field){
$( #results )。append(field。 value + );
});
}

$( :checkbox,:radio ).click(showValues);
showValues();
$(' #display')。click(function(n){
document.location.href = trialIndex.php
});
});





非常感谢您的帮助!

解决方案

(function(){
var tableLenght = new Array();


' #matrix')。click(function(){
var counter = 0 ;


(< span class =code-string> #riskForm tr)。each(function(){
// 忽略表格标题(标题)
if (counter > = 1 ){
tableLenght.push(

I am trying to create a matrix table. The values to be plugged into the matrix will be user input from an html table (basically a unique pairing table which pairs two elements at a time and for each pair a user can select which he prefers and by how much it is prefered on a scale of 1-9 using radio buttons). it is the preferences i.e. 1-9 that gets plugged into the matrix table and the number of unique pairing that detemines the lenght of the matrix.

The problem is now i want to get the values of clicked radios into the matix. I know how to get it but just don't know how to fit it into the matrix. Here is the code I have for now, pls ask me questions if needed thanks:

$(function(){
    var tableLenght = new Array();
    $('#matrix').click(function(){
    var counter = 0;
    $("#riskForm tr").each(function(){
        //ignores the header of the table (the title)
    if(counter >=1){
        tableLenght.push($(this).closest('tr').children("td:eq(1)").text());
    }
    counter++;
    });

// get unique attributes of in our table
var uniqueList = new Array();
//push first element onto list
uniqueList.push(tableLenght[0]); //pushes current elem onto the array
var cur = tableLenght[0]; //sets first element by default
for(var i = 1; i < tableLenght.length; i++){ //loops through the whole lenght of our array
    if(cur != tableLenght[i]){//checks if the current is not same as the next one
            cur = tableLenght[i]; //sets the new elemt
    uniqueList.push(tableLenght[i]); //pushes onto the array
    }
}
alert(uniqueList); //alerts only the unique table rows in the tableLenght array
var myArray = new Array(uniqueList);

    for (var i = 0; i < uniqueList; i++) {
        myArray[i] = new Array(uniqueList);
        for (var j = 0; j < uniqueList; j++) {
            myArray[i][j] = '';
        }
}
});

//to show/get the values of selected radio buttons
function showValues() {
    var fields = $( ":input").serializeArray();
    $( "#results" ).empty();
    jQuery.each( fields, function( i, field ) {
    $( "#results" ).append( field.value + " " );
    });
}

$( ":checkbox, :radio" ).click( showValues );
showValues();
$('#display').click(function (n) {
document.location.href="trialIndex.php"
    });
});



Your help is much appreciated!

解决方案

(function(){ var tableLenght = new Array();


('#matrix').click(function(){ var counter = 0;


("#riskForm tr").each(function(){ //ignores the header of the table (the title) if(counter >=1){ tableLenght.push(


这篇关于使用javascript创建多维数组(nXn)矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆