gpt4 book ai didi

Javascript:如果 CSV 中没有,则将值添加到 CSV - 如果已经在 CSV 中 - 从 CSV 中删除

转载 作者:行者123 更新时间:2023-11-28 10:20:58 29 4
gpt4 key购买 nike

我正在构建一个 CSV 字符串 - 因此用户单击 div 的所有内容 - 5 个字符的字符串都会传递到隐藏字段中 - 我想做的是附加每个新值并创建一个 CSV 字符串 - 完成后- 在文本框中显示 - 如果传递到 CSV 的值已存在 - 将其删除,然后像以前一样在文本字段中显示 CSV。我有以下内容,但它不起作用...

function newQuery(val, id) {

var xy = document.getElementById(id);
var x = parent.document.getElementById('txtCriteria');
var myValue = parent.document.getElementById('txtCriteria').value;
var hdn = document.getElementById("hdnSearch");
hdn.value += "," + val;

var element = hdn.value;
var fin;

var leadingComma = element.substring(1, 0).toLowerCase();
var trailingComma = element.substring(element.length - 1);


// Check for leading comma
if (leadingComma == ",") {
fin = element.substring(1);
}
// Check for trailing comma
if (element.charAt(element.length - 1) == ",") {
fin = element.slice(0, -1);
}

if (x.value.search(val) == 0) {
alert('Already exists');

fin.replace(val, "");
x.value = fin;

// Set image to checked
xy.src = 'bullet_plus.png';

}
else if (element.search(val) > 0) {
alert('New Selection: ' + fin);

x.value = fin;

// Set image to checked
xy.src = 'bullet_tick.png';

}
}

最佳答案

处理 CSV 时,我建议您使用 Array.joinString.split

var foo = "hello, there, so";
var arr = foo.split(","); // ["hello", " there", "so"];
var bar = arr.join(","); // "hello, there, so"

对于您的代码来说,如果没有 HTML 或没有有用的变量名称,它就没有意义。

关于Javascript:如果 CSV 中没有,则将值添加到 CSV - 如果已经在 CSV 中 - 从 CSV 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690494/

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