我一直在研究测试数组中是否存在值,并不断获得 indexOf('textvalue') 的匹配项。
但是,我的 'textvalue' 将是一个子字符串,因此不会匹配。这是因为我将字段名称传递给方法,只需删除“门:左前;” 文本通过匹配“门”上的数组项。有谁知道我怎么能做到这一点?
这是我正在处理的示例代码:
(spnSelectedDisclosures 将有一个选定字段的列表,这是从文本中删除前一个字段选择的代码,它是用分号分隔的。)
var currentText = $("#spnSelectedDisclosures").text();
var existingArr = currentText.split(";")
for (i=0;i < existingArr.length;i++) {
var indexItem = " " + existingArr[i].toString(); // why is this still an object, instead of a string? Adding a space to it was a desperate try to make indexItem a string variable.
if (indexItem.contains(substringText)) { // offending line, saying the object has no method 'contains'
alert("there's been a match at: " + i);
textToRemoveIndex = i;
break;
}
}
var textToRemove = existingArr[textToRemoveIndex];
var newText = currentText.replace(textToRemove,"");
$("#spnSelectedDisclosures").text(newText);