gpt4 book ai didi

c# - 将 JQuery 转换为 ForEach() 循环会引发错误

转载 作者:行者123 更新时间:2023-12-01 08:40:11 24 4
gpt4 key购买 nike

我正在运行以下语法,该语法仅适用于一个硬编码的网格名称。我想将其转换为迭代网格数组(因为另外 3 个网格刚刚添加到页面中),但出现错误

System.Data.SqlClient.SqlException: 'The parameterized query expects the parameter '@slm', which was not supplied.'

现在这是适用于一个网格的语法

    <script>
$("#btnSave").click(function () {
var GvonedataList = [];
var slm = false;
var prs = false;
var rgs = false;
var hsp = false;
var ent = false;
$('[id*=gvalpha]').find('tr:has(td)').each(function () {
var gridrow = $(this);
var colno = 2;
var Gvonedata = {};
slm = true;
Gvonedata.fldslm = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
$('#cbxslm input[type=checkbox]').each(function () {
if ($(this).prop('checked')) {
if ($(this).val() == 'prs') {
prs = true;
Gvonedata.fldprs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'rgs') {
rgs = true;
Gvonedata.fldrgs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'hsp') {
hsp = true;
Gvonedata.fldhsp = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'ent') {
ent = true;
Gvonedata.fldent = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
}
});
GvonedataList.push(Gvonedata);
});
if (GvonedataList.length > 0) {
$.ajax({
type: 'POST',
url: '<%= ResolveUrl("estingAjax.aspx/SaveGridData") %>',
data: '{Gvonedata: ' + JSON.stringify(GvonedataList) + ', slm: ' + slm + ', prs: ' + prs + ', rgs: ' + rgs + ', hsp: ' + hsp + ', ent: ' + ent + '}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
});
</script>

这是引发上述错误的语法

    <script>
$("#btnSave").click(function () {
var GvonedataList = [];
var slm = false;
var prs = false;
var rgs = false;
var hsp = false;
var ent = false;
var grids = ["gridfirst", "gridsecond", "gridthird", "gridfourth"];
var gridlen = grids.length;
var i;
for (i = 0; i < gridlen; i++) {
var gridrow = $(this);
var colno = 2;
var Gvonedata = {};
slm = true;
Gvonedata.fldslm = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
$('#cbrst input[type=checkbox]').each(function () {
if ($(this).prop('checked')) {
if ($(this).val() == 'prs') {
prs = true;
Gvonedata.fldprs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'rgs') {
rgs = true;
Gvonedata.fldrgs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'hsp') {
hsp = true;
Gvonedata.fldhsp = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'ent') {
ent = true;
Gvonedata.fldent = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
}
});
GvonedataList.push(Gvonedata);
if (GvonedataList.length > 0) {
$.ajax({
type: 'POST',
url: '<%= ResolveUrl("estingAjax.aspx/SaveGridData") %>',
data: '{Gvonedata: ' + JSON.stringify(GvonedataList) + ', slm: ' + slm + ', prs: ' + prs + ', rgs: ' + rgs + ', hsp: ' + hsp + ', ent: ' + ent + '}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
};
});
</script>

如何更改第二个代码以使其迭代数组并插入?

最佳答案

在下面的行中 $(this) 返回被单击的按钮:

var gridrow = $(this);

您应该从当前表中获取一个表行,就像在初始代码中对 1 个表所做的那样,例如:

<script>
$("#btnSave").click(function () {
var GvonedataList = [];
var slm = false;
var prs = false;
var rgs = false;
var hsp = false;
var ent = false;
var grids = ["gridfirst", "gridsecond", "gridthird", "gridfourth"];
var gridlen = grids.length;
var i;
for (i = 0; i < gridlen; i++) {
GvonedataList = [];
slm = false;
prs = false;
rgs = false;
hsp = false;
ent = false;
$('[id*='+grids[i]+']').find('tr:has(td)').each(function () {
var gridrow = $(this);
var colno = 2;
var Gvonedata = {};
slm = true;
Gvonedata.fldslm = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
$('#cbrst input[type=checkbox]').each(function () {
if ($(this).prop('checked')) {
if ($(this).val() == 'prs') {
prs = true;
Gvonedata.fldprs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'rgs') {
rgs = true;
Gvonedata.fldrgs = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'hsp') {
hsp = true;
Gvonedata.fldhsp = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
else if ($(this).val() == 'ent') {
ent = true;
Gvonedata.fldent = gridrow.find("td:nth-child(" + colno + ")").html();
colno = colno + 1;
}
}
});
GvonedataList.push(Gvonedata);
});
if (GvonedataList.length > 0) {
$.ajax({
type: 'POST',
url: '<%= ResolveUrl("estingAjax.aspx/SaveGridData") %>',
data: '{Gvonedata: ' + JSON.stringify(GvonedataList) + ', slm: ' + slm + ', prs: ' + prs + ', rgs: ' + rgs + ', hsp: ' + hsp + ', ent: ' + ent + '}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
}
});
</script>

UPD:此外,每次迭代时所有变量都应设置回默认值。

关于c# - 将 JQuery 转换为 ForEach() 循环会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254664/

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