JS仅在替换第一个值时才替换数组中的值

问题描述

我有一个网页正在尝试选择和替换/删除一个值。当我单击选择时,在控制台中显示它正在选择我要选择的项目,但是当在splice命令中输入值时,它只会更改数组的第一个条目。

第一个功能是根据位置选择条形,截面函数是条形数组值,条形和按钮出现的位置,如果值是-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,parseInt(replaceBox.value));

它应该从长度为1的barNum位置开始,并取下替换框的int值,并使用它来拼接值

编辑- 我在替换Screenshot of page之前和2nd Screenshot

之后添加了页面的图像

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)