gpt4 book ai didi

javascript - 更新 Javascript 中的数组元素

转载 作者:行者123 更新时间:2023-11-28 03:37:37 24 4
gpt4 key购买 nike

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

如果我调用 newProp() 函数并注释掉确定是调用 newProp() 还是命定 Prop() 的 IF 语句,我的函数(位于页面底部)运行良好。如果我保留 IF 语句,如果我将任何必填字段留空,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”,则输入表单的任何数据都将使用推送附加到数组中。否则,表单已经填充了数组元素,因此函数应该覆盖这些元素。

进行编辑以简化代码

最佳答案

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

因此,该表单有一个“属性 ID”字段,其值为“TBD”。单击表单的第一个字段后,“属性 ID”字段的值将更新为 1。每次保存表单时,变量都会递增并将该字段设置回 TBD。下次选择第一个字段时,它会将属性 ID 更新为值 2,依此类推。此外,如果我记忆起现有记录,所有字段(包括属性 ID)都将填充从数组中提取的记录详细信息。这意味着,在任何时候,当我单击调用 newProperty() 函数的按钮时,“属性 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();
}

现在它正在工作。

关于javascript - 更新 Javascript 中的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57574529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com