我正在运行以下语法,该语法仅适用于一个硬编码的网格名称。我想转换它来迭代一个网格数组(因为刚刚添加了3个额外的页面)但是我得到了错误
System.Data.SqlClient.SqlException:'参数化查询需要参数' @ slm',这是未提供的。'
现在这是适用于一个网格的语法
<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>
如何选择第二个代码来迭代数组并插入?
答案 0 :(得分:2)
在以下行中,$(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:所有变量也应该在每次迭代时都设置为默认值。