我需要将aspx页面中gridview的新选定行索引传递给我的javascript。但是代码只能获取变量的初始值,并且当脚本再次运行时,它包含未更新的值。在我的代码从后面的代码通过变量到javascript不更新
变量后面是“SelectedRowIndex”,它是一个整数
window.addEventListener("keydown", function (event) {
console.log('<%= SelectedRowIndex %>');
var validArrowKey = false;
var index = '<%= SelectedRowIndex %>';
if (event.keyCode == 40) {
validArrowKey = true;
index++;
}
else if (event.keyCode == 38 && index > -1) {
validArrowKey = true;
index--;
}
if (validArrowKey) {
var trPaymentDetails = document.getElementById("trPaymentDetails_" + index.toString())
if (trPaymentDetails) {
__doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());
}
else
__doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());
}
}, false);
要设置所选择的行索引的初始值,用户必须单击在GridView的行首先,在该触发器内部将变量设置为用户选择的行。
您刚刚分配了一个匿名函数,每次发生keydown事件时都会执行该函数。所以每次都会执行里面的变量声明。这就是为什么它始终是初始值。尝试将索引声明为全局变量。 –
用户首先必须单击gridview中的一行来设置第一个索引。 –