(JS) 拼接替换数组中的值,只替换第一个值

(JS) Splice to replace a value in an array only ever replacing the first value

我有一个网页,我正在尝试 select 和 replace/delete 一个值。当我单击 select 时,在控制台中它显示我正在 selecting 我想要 select 的项目,但是当将值输入到拼接命令时,它只会更改数组的第一个条目。

第一个函数是select基于位置的条形图,section函数是条形图数组值的位置,然后出现一个条形图和按钮,如果值为-1,它将删除该值如果没有,它将取代它

    function clickBar(Event) {

    //Receives position in xy for user clicks
    var posX = Event.clientX;
    var posY = Event.clientY;
    console.log("X = "+posX +" Y = "+ posY);

    var barWidth= 500/barVals.length;
    console.log("Bar Width = "+barWidth);
    var barNum;

    //checks to see if click was within the confines of the area that the boxes are displayed
    //if so barNum is calculated to show which position in the array you are clicking on

    if(posY >topY && posY < bottomY && posX > leftX && posX < rightX){
        console.log("Inside");
        barNum = Math.floor((posX - leftX) / barWidth);
        console.log("Bar Number = "+barNum);
    } else {
        console.log("Outside");}

    if (barNum > -1) {
        replaceBar(barNum)
    }

    console.log(barVals);
    draw();
}

document.addEventListener("click", clickBar);

function replaceBar(barNum) {

    console.log("Bar Number2 = "+barNum);
    var replaceBox = document.getElementById('replaceVal');
    var replaceBtn = document.getElementById('replaceBtn');

    var displayBoxSetting = replaceBox.style.display;
    var displayBtnSetting = replaceBtn.style.display;

    //hiding and displaying the edit text box and button
    if (displayBoxSetting == 'block' && displayBtnSetting=='block') {
        replaceBox.style.display = 'none';
        replaceBtn.style.display = 'none';
    }
    else {
        replaceBox.style.display = 'block';
        replaceBtn.style.display = 'block';
    }

    //Used to replace the value of the
    if(replaceBox.value >0) {
        barVals.splice(barNum, 1, parseInt(replaceBox.value));
        colours.push(document.querySelector('input[name="colour"]:checked').value);
        replaceBox.value = 0;
        draw(); // redraw

    }
    else if(replaceBox.value == -1){
        barVals.splice(barNum, 1);
        replaceBox.value = 0;
        draw(); // redraw
    }
    textBoxObj.value = 0;
    replaceBox.style.display = 'none';
    replaceBtn.style.display = 'none';
    draw(); // redraw
}

问题是:

if(replaceBox.value >0) {
    barVals.splice(barNum, 1, parseInt(replaceBox.value));

应该是从barNum位置开始,长度为1,取replace box的int值,拼接值

编辑 - 我在替换 Screenshot of page and after 2nd Screenshot

之前包含了该页面的图像

好吧,我似乎以完全错误的方式开始了这件事,所以这里有一个足够不同的解决方案。这是放在 draw 函数中,取自 canvas 本身。

canvas.onclick = function mousePos(Event) {


    var rect = canvas.getBoundingClientRect();
    var posX = Event.clientX- rect.left;
    var posY = Event.clientY - rect.height;
    console.log("X = "+posX +" Y = "+ posY);

    var barWidth= 500/barVals.length;

    console.log("Bar Width = "+barWidth);

    var barNum;

    barNum = Math.floor((posX-60) / barWidth);

if(barNum>=0 && barNum < barVals.length) {
    var position = barNum;
    console.log(position);
    var amount = prompt("Please enter the new value", "734");
    //barVals[position] = amount;

    if(amount >0) {
        barVals.splice(position, 1, parseInt(amount));
        console.log(amount);
        draw(); // redraw

    }
    else if(amount = -1){
        barVals.splice(position, 1);
        colours.splice(position, 1)
        draw(); // redraw
    }else{
        alert("Incorrect value entered");
    }
}

}