简体   繁体   English

在定义后停止更新值

[英]Stopping a value from being updated after being defined

I'm trying to have it so that upon the start of a function an "oldvalue" is created that is based on the contents of a text field, since I want certain textfields to be updated automatically until edited.我试图让它在 function 开始时创建一个基于文本字段内容的“旧值”,因为我希望某些文本字段在编辑之前自动更新。 So the thought behind it is that I compare the oldvalue to the value currently in the textbox that is editable and if they don't match, to stop editing that field.所以它背后的想法是我将旧值与文本框中当前可编辑的值进行比较,如果它们不匹配,则停止编辑该字段。 However when I define the oldvalue based on a non-editable textbox it keeps updating when that box is changed automatically.但是,当我基于不可编辑的文本框定义 oldvalue 时,它会在该框自动更改时不断更新。 In other words, how do I stop a value from updating after first setting it?换句话说,如何在第一次设置值后停止更新?

function DescChanged() {
            desc = document.getElementById("Txb_Description");
            input = document.getElementById("Txb_WebArtName");
            kortinput = document.getElementById("KortBesc");
            langinput = document.getElementById("LangBesc");
            merk = document.getElementById("Brand");
            bestelnr = document.getElementById("Txb_ArticleOrderNr");
            const inputold = input;

With this if clause:有了这个 if 子句:

if (kortinput.value == inputold.value || isEmptyOrSpaces(kortinput.value))

Try having a variable outside the function, and use a temporary variable within the function, as:尝试在 function 之外使用一个变量,并在 function 中使用一个临时变量,如下所示:

var inputold = "";
function DescChanged() {
        desc = document.getElementById("Txb_Description");
        input = document.getElementById("Txb_WebArtName");
        kortinput = document.getElementById("KortBesc");
        langinput = document.getElementById("LangBesc");
        merk = document.getElementById("Brand");
        bestelnr = document.getElementById("Txb_ArticleOrderNr");
        // check if values are equal
        if (kortinput.value == inputold.value || isEmptyOrSpaces(kortinput.value))
            const inputold = input; // update the oldvalue
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM