【发布时间】: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