【问题标题】:Update array elements in Javascript在 Javascript 中更新数组元素
【发布时间】:2019-12-25 17:21:46
【问题描述】:

我觉得我在这里忽略了一些非常简单的事情,但我不明白为什么会失败。

如果我调用 newProp() 函数并注释掉确定是否调用 newProp() 或 existingProp() 的 IF STATEMENT,我的函数(在页面底部)运行良好。如果我保留IF STATEMENT,那么如果我将任何必填字段留空,existingProp() 将成功确认(通过警告框)。如果我填写了所有必填字段,existingProp() 似乎没有做任何事情。看起来它没有使用 properties[currentArrayID][x] 代码更新我的数组,它应该用存储在变量中的新信息集覆盖 properties[currentArrayID]。

properties[currentArrayID][0] = currentPID;
properties[currentArrayID][1] = number;
properties[currentArrayID][2] = street;
properties[currentArrayID][3] = suburb;
properties[currentArrayID][4] = postcode;
properties[currentArrayID][5] = status;
properties[currentArrayID][6] = owner;
properties[currentArrayID][7] = ownernum;
properties[currentArrayID][8] = tenant;
properties[currentArrayID][9] = tenantnum;

document.frmPropData.txtNumber.value = "";
document.frmPropData.txtStreet.value = "";
document.frmPropData.txtSuburb.value = "";
document.frmPropData.txtPostcode.value = "";
document.frmPropData.drpStatus.value = "NA";
document.frmPropData.txtOwner.value = "";
document.frmPropData.txtOwnerNum.value = "";
document.frmPropData.txtTenant.value = "";
document.frmPropData.txtTenantNum.value = "";
document.frmPropData.txtPID.value = "TBD";

但是一旦我尝试将它包含在我的函数中,该函数就会停止工作。 完整功能如下:

var properties = [];
var i = 0;
var x = 1;

var number = "";
var street = "";
var suburb = "";
var postcode = "";
var status = "";
var owner = "";
var ownernum = "";
var tenant = "";
var tenantnum = "";
var propID = "";

var tenantDetails = "";

var currentPID = "";
var currentArrayID = "";

function newProperty() {
    number = document.frmPropData.txtNumber.value;
    street = document.frmPropData.txtStreet.value;
    suburb = document.frmPropData.txtSuburb.value;
    postcode = document.frmPropData.txtPostcode.value;
    status = document.frmPropData.drpStatus.value;
    owner = document.frmPropData.txtOwner.value;
    ownernum = document.frmPropData.txtOwnerNum.value;
    tenant = document.frmPropData.txtTenant.value;
    tenantnum = document.frmPropData.txtTenantNum.value;
    propID = x;


    if (tenant != "") {
        tenantDetails = tenant + " - " + tenantnum
    } else {
        tenantDetails = "Not Applicable"
    }

    //store value of current PropertyID
    currentPID = document.frmPropData.txtPID.value;
    currentArrayID = currentPID - 1;

    //check if PropertyID already exists
    if (currentPID != "TBD") {
        existingProp();
    } else { 
        newProp();
    }
}

function existingProp() {
    //check for blank entries
    if (number != "" && street != "" && suburb != "" && postcode != "" && status != "NA" && owner != "" && ownernum != "") {
        properties[currentArrayID][0] = currentPID;
        properties[currentArrayID][1] = number;
        properties[currentArrayID][2] = street;
        properties[currentArrayID][3] = suburb;
        properties[currentArrayID][4] = postcode;
        properties[currentArrayID][5] = status;
        properties[currentArrayID][6] = owner;
        properties[currentArrayID][7] = ownernum;
        properties[currentArrayID][8] = tenant;
        properties[currentArrayID][9] = tenantnum;

        document.frmPropData.txtNumber.value = "";
        document.frmPropData.txtStreet.value = "";
        document.frmPropData.txtSuburb.value = "";
        document.frmPropData.txtPostcode.value = "";
        document.frmPropData.drpStatus.value = "NA";
        document.frmPropData.txtOwner.value = "";
        document.frmPropData.txtOwnerNum.value = "";
        document.frmPropData.txtTenant.value = "";
        document.frmPropData.txtTenantNum.value = "";
        document.frmPropData.txtPID.value = "TBD";
        alert("no blanks found")
    } else {
        alert("Please complete all fields marked with an asterisk *")
    }
}

为了提供更多上下文,此函数检测名为 txtPID 的表单字段中的值并将其存储在 currentPID 中。该值从 1 开始,因此另一个名为 currentArrayID 的变量等于 currentPID 减 1,以确定数组索引。如果 txtPID 字段值为“TBD”,则输入到表单中的任何数据都将使用推送附加到数组中。否则,表单已经填充了数组元素,所以函数应该覆盖这些元素。

经过编辑以简化代码

【问题讨论】:

  • properties 来自哪里?
  • properties 是我的数组的名称。它是全局定义的。
  • 它包含什么?控制台有错误吗?
  • 请告诉我们你是如何调用函数的。
  • the function ceases to work是什么意思

标签: javascript arrays overwrite


【解决方案1】:

我现在已经解决了这个问题。

所以表单有一个“属性 ID”字段,其值为“TBD”。单击表单的第一个字段后,Property ID 字段的值会更新为 1。每次保存表单时,变量都会递增并将字段设置回 TBD。下次选择第一个字段时,它将属性 ID 更新为值 2,依此类推。 此外,如果我回想现有记录,包括属性 ID 在内的所有字段都将填充从数组中提取的记录的详细信息。这意味着,在任何时候,当我单击调用newProperty() 函数的按钮时,Property ID 字段的值都不会是“TBD”。所以我不得不检查数组索引是否存在。

所以我变了

//check if PropertyID already exists
    if (currentPID != "TBD") {
        existingProp();
    } else { 
        newProp();
    }

    //check if PropertyID already exists
    if (typeof properties[currentArrayID] ==='undefined') {
        newProp();
    } else { 
        existingProp();
    }

它现在正在工作。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 2012-04-16
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2013-09-07
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多