我正在尝试创建一个矩阵表。插入矩阵的值将是来自html表的用户输入(基本上是一个唯一的配对表,它一次配对两个元素,每个用户可以选择他喜欢哪个,以及在一个比例上优先选择多少1-9使用单选按钮)。它是首选项,即插入矩阵表的1-9和确定矩阵长度的唯一配对数。
当生成matric时我想要这样的东西:
0 1 2 3 4 sum
1 [1] [6] [2] [8] 17
2 [ ] [1] [9] [4] 15
3 [ ] [ ] [1] [7] 8
4 [ ] [ ] [ ] [1] 1
问题是现在我想将点击的无线电的值放入matix。我知道如何得到它,但只是不知道如何将其融入矩阵。这是我现在的代码,如果需要,请问我问题谢谢:
$(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"
});
});
非常感谢您的帮助!
答案 0 :(得分:2)
您可以将值推送到数组,例如:
var a = [];
a.push(1);
a.push(2);
console.log(a); // [1, 2]
您可以根据索引进行设置:
var a = [];
a[0] = 1;
a[1] = 2;
console.log(a); // [1, 2]
要创建多维数组,只需将数组推送到一个数组即可。
var a = [1,2,3];
var b = [4,5,6];
var c = [];
c.push(a);
c.push(b);
console.log(c); // [[1, 2, 3], [4, 5, 6]]
答案 1 :(得分:1)
我认为您正在尝试在此处实施AHP算法。在这里编写整个代码以及它如何工作是相当复杂的,但这里是已经退出的AHP工作的链接。希望它有所帮助:https://github.com/humbertoroa/ahp