简体   繁体   English

未捕获的SyntaxError:意外的标识符

[英]Uncaught SyntaxError: Unexpected identifier

I'm working on a simple function whereby I have a table of rows and on each row is an editButton. 我正在使用一个简单的函数,其中有一个行表,每行上都有一个editButton。 In the onclick of the edit button, I am calling the bellow function and getting the error: 在编辑按钮的onclick中,我正在调用波纹管函数并收到错误消息:

Uncaught SyntaxError: Unexpected identifier 

It points to line 1 of the php file which just has the doctype declaration (which seems to be fine when I load the page first - the error only occurs when I click on an edit button). 它指向仅具有doctype声明的php文件的第1行(当我第一次加载页面时,这似乎很好-仅在单击编辑按钮时才会发生错误)。 Function below: 功能如下:

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>';
    };

Initial html is dynamically appended with javascrript and appears correctly on screen: 初始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 declaration; Doctype声明;

<!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">

Any help appreciated, I cannot spot the error 感谢任何帮助,我无法发现错误

I think your problem is being caused due to this line: 我认为您的问题是由于以下原因引起的:

onClick="editGUI('+ i +','+JSON_csw[i].carerName+','+JSON_csw[i].carerAddress+', '+JSON_csw[i].carerMobile+', '+JSON_csw[i].carerID+');">\

When you are sending parameters to the editGUI function in your click handler, you do not have quotes to denote strings. 当您将参数发送到单击处理程序中的editGUI函数时,没有引号表示字符串。 For a sample element in JSON_csw, this is how that line is appended to the html: 对于JSON_csw中的示例元素,这是将该行添加到html的方式:

onClick="editGUI(1,test_name,test_address,test_mobile, 5)"

You should enclose the strings in quotes, like: 您应该将字符串括在引号中,例如:

onClick="editGUI('+ i +',\''+JSON_csw[i].carerName+'\',\''+JSON_csw[i].carerAddress+'\', \''+JSON_csw[i].carerMobile+'\', '+JSON_csw[i].carerID+');">\

I think it would then work correctly. 我认为这样可以正常工作。 I haven't tried the above code too, use Inspect Element to see how the onClick handler is actually being shown in the DOM. 我也没有尝试过上面的代码,请使用Inspect Element来查看onClick处理程序在DOM中的实际显示方式。 Then use appropriate escaping and you should be good to go. 然后使用适当的转义,您应该一切顺利。

Unless carerID is a number, it must be quoted. 除非carerID是数字,否则必须用引号引起来。 Something like: 就像是:

 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>';

Note: I did not try this code. 注意:我没有尝试此代码。 You may have to play around with escaping the quotes. 您可能需要绕开引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM