如何合并两个不同的数组值

时间:2018-06-19 09:00:32

标签: javascript jquery

我试图将两个不同的数组值合并为“:”分开,但我错过了一些逻辑功能,plz建议以下情况。

预计会发送值:

35:2:1`45:1:1`45:1:1`45:2:1`45:1:1`34`33`33`34`33`33`34`33`33:1:2`34

HTML代码

<div id="cwRow1" class="cwRow">
    <label>Row1:</label>
    <div id="cwCol1" class="cwCol">
        <input type="text" class="width100">
        <input type="number" placeholder="1" class="rcHeight">
        <input type="number" placeholder="1" class="rcWidth">
    </div>
    <div id="cwCol2" class="cwCol">
        <input type="text" class="width100">
        <input type="number" placeholder="1" class="rcHeight">
        <input type="number" placeholder="1" class="rcWidth">
    </div>
</div>

使用Javascript:

var totalColumns = $(".width100");
var csvalue = $(".rcWidth, rcHeight");
var svalue = [];
for (var j = 0; j < totalColumns.length; j++) {
    colInput[increm] = parseFloat(totalColumns[0].value);

    for (var j = 0; j < csvalue.length; j++) {
        svalue[increm] = parseFloat(totalColumns[0].value);
        increm++;
    }
    increm++;
}

1 个答案:

答案 0 :(得分:1)

以下是cwCol div上使用jQuery each作为容器的示例:

&#13;
&#13;
var arr = [];

$('.cwCol').each(function(event ,i){
  var width = $(this).find('.width100').eq(0).val();
  var rcHeight = $(this).find('.rcHeight').eq(0).val();
  var rcWidth = $(this).find('.rcWidth').eq(0).val();
  arr.push(width+":"+rcHeight+":"+rcWidth);
})

console.log(arr.join('`'));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cwRow1" class="cwRow">
    <label>Row1:</label>
    <div id="cwCol1" class="cwCol">
        <input type="text" class="width100" value="35">
        <input type="number" placeholder="1" class="rcHeight" value="1">
        <input type="number" placeholder="1" class="rcWidth" value="2">
    </div>
    <div id="cwCol2" class="cwCol">
        <input type="text" class="width100" value="45">
        <input type="number" placeholder="1" class="rcHeight" value="3">
        <input type="number" placeholder="1" class="rcWidth" value="4">
    </div>
</div>
&#13;
&#13;
&#13;