这个JS代码中的错误是什么
what is error in this js code
var RowNumber = row.rowIndex;
alert(RowNumber);
var a = document.createElement('a');
a.textContent = 'Remove';
a.style.cursor = 'pointer';
//a.href = "javascript: document.getElementById(tblId).deleteRow(0);";
a.setAttribute("onclick","alert(RowNumber)");
cell1.appendChild(a);
火虫说"未定义行号",但我在上面定义了行号。还使用 RowNumber 变量。
我的完整函数是 =
function addRowToTable(tblId,icOptionArray,leavRowFromBottom,selectedValue,selectedQuantity)
{
var tbl = document.getElementById(tblId);
var lastRow = tbl.rows.length - (leavRowFromBottom + 0);
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
// var iteration = lastRow + 1;
var row = tbl.insertRow(lastRow);
// cell 0
var cell0 = row.insertCell(0);
cell0.align='right';
var el = document.createElement('label');
el.textContent = 'IC Chips :';
cell0.appendChild(el);
//cell 1
var cell1 = row.insertCell(1);
var selector = document.createElement('select');
selector.id = 'icchips';
selector.name = 'icchips[]';
selector.style.width = '200px';
//alert(selector);
for (var key in icOptionArray)
{
if(key == 'containsValue') continue;
var option = document.createElement('option');
if(key == selectedValue)
option.selected = true;
option.value = key;
option.appendChild(document.createTextNode(icOptionArray[key]));
selector.appendChild(option);
}
cell1.appendChild(selector);
var space = document.createTextNode(' ');
cell1.appendChild(space);
var lbl = document.createElement('label');
lbl.textContent = 'Qty :';
cell1.appendChild(lbl);
var selector = document.createElement('select');
selector.id = 'additional_quantity';
selector.name = 'additional_quantity[]';
for (var key in QUANTITIES_ARR)
{
if(key == 'containsValue') continue;
var option = document.createElement('option');
if(key == selectedQuantity)
option.selected = true;
option.value = key;
option.appendChild(document.createTextNode(QUANTITIES_ARR[key]));
selector.appendChild(option);
}
cell1.appendChild(selector);
var space = document.createTextNode(' ');
cell1.appendChild(space);
var RowNumber = row.rowIndex;
alert(RowNumber);
var a = document.createElement('a');
a.textContent = 'Remove';
a.style.cursor = 'pointer';
//a.href = "javascript: document.getElementById(tblId).deleteRow(0);";
a.setAttribute("onclick","alert(RowNumber)");
cell1.appendChild(a);
}
这样做
a.onclick = function(){ alert(RowNumber); };
问题是定义RowNumber
的本地作用域与调用onclick
处理程序的作用域不同。@jancha提出的解决方案解决了这个问题。他对闭包的使用可确保处理程序中的 RowNumber 与本地作用域中的 RowNumber 相同。
RowNumber
没有在创建元素的范围内定义,因为您使用 jQuery 您可以使用:
var RowNumber = row.rowIndex;
alert(RowNumber);
var a = document.createElement('a');
a.textContent = 'Remove';
a.style.cursor = 'pointer';
//a.href = "javascript: document.getElementById(tblId).deleteRow(0);";
$(a).click(function(e){
alert(RowNumber);
});
相关文章:
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 处理浮点错误的最佳方法是什么
- 这个JS代码中的错误是什么
- 角度中的这个错误是什么
- ng路由不起作用.我不知道错误是什么,因为控制台中没有显示任何内容
- 这个ajax代码中的错误是什么
- win.js.ui.html控件:'元素'未定义.?这个错误是什么
- 这个语法错误是什么?(测试Hapi.js)
- JSP内部的Java代码中的错误是什么
- JavaScript中数组的错误是什么?
- 函数在传递数据给PHP文件时没有定义,错误是什么?
- 这个错误是什么"[$compile:nonassign]"在angularjs指令中
- 这个对象合并函数中可能出现的错误是什么?
- JavaScript代码中的错误是什么
- 这个AJAX jQuery的错误是什么?
- Jquery代码不起作用,但JavaScript起作用.我的错误是什么
- JSLint错误“是什么;类型错误"意思是
- 我的代码中的错误是什么?变量的乘法没有显示在总数中
- 当openFaces组件中的validationError=true时,具体的验证错误是什么?
- 当我试图通过Ajax传递值到php文件时,我的代码中的错误是什么