我正在开发一个简单的函数,其中我有一个行表,每行都有一个编辑按钮。在编辑按钮的 onclick 中,我调用以下函数并收到错误:
Uncaught SyntaxError: Unexpected identifier
它指向 php 文件的第 1 行,该文件只有 doctype 声明(当我首先加载页面时似乎没问题 - 仅当我单击编辑按钮时才会发生错误)。函数如下:
function editGUI(rowNum, carerName, carerAddress, carerMobile, carerID){
row=document.getElementById('Row'+rowNum);
row.innerHTML= '<tr id="Row' + i + '">\
<td><input type="text" value="' + carerName + '"></td>\
<td><input type="text" value="' + carerAddress + '"></td>\
<td><input type="text" value="' + carerMobile + '"></td>\
<td> HISTORY </td>\
<td><button onClick="editCarer('+carerID+');">EDIT</button></td>\
</tr>';
};
初始 html 动态附加 javascrript 并正确显示在屏幕上:
for (var i = 0; i < JSON_csw.length; i++)
{
table += '<tr id=Row'+ i +'>\
<td>' + JSON_csw[i].carerName + '</td>\
<td>' + JSON_csw[i].carerAddress + '</td>\
<td>' + JSON_csw[i].carerMobile + '</td>\
<td> HISTORY </td>\
<td><button id=btn[' + i + '] \
onClick="editGUI('+ i +','+JSON_csw[i].carerName+','+JSON_csw[i].carerAddress+', '+JSON_csw[i].carerMobile+', '+JSON_csw[i].carerID+');">\
EDIT</button></td>\
</tr>';
}
文档类型声明;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
感谢任何帮助,我无法发现错误
最佳答案
我认为您的问题是由这一行引起的:
onClick="editGUI('+ i +','+JSON_csw[i].carerName+','+JSON_csw[i].carerAddress+', '+JSON_csw[i].carerMobile+', '+JSON_csw[i].carerID+');">\
当您将参数发送到单击处理程序中的 editGUI 函数时,没有引号来表示字符串。对于 JSON_csw 中的示例元素,这就是该行附加到 html 的方式:
onClick="editGUI(1,test_name,test_address,test_mobile, 5)"
您应该将字符串括在引号中,例如:
onClick="editGUI('+ i +',\''+JSON_csw[i].carerName+'\',\''+JSON_csw[i].carerAddress+'\', \''+JSON_csw[i].carerMobile+'\', '+JSON_csw[i].carerID+');">\
我认为它会正常工作。我也没有尝试过上面的代码,请使用 Inspect Element 来查看 onClick 处理程序实际上是如何在 DOM 中显示的。然后使用适当的转义,你就可以开始了。
关于javascript - 未捕获的语法错误 : Unexpected identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935677/