JavaScript onclick passes an object parameter (when Easyui passes a row of data) Error: uncaught syntaxerror:unexpected identifier
Bloggers encountered when passing object parameters with the onclick (when Easyui passed a row of data), Sncaught syntaxerror:unexpected identifier error is reported.
It is found that the onclick(对象)
object in the form of this transitive object becomes onclick([object Object])
.
- Workaround: Convert the object to a JSON string, and then translate the double quotation marks of the JSON string into single quotes.
- Idea: Json.stringify (). replace (/\ "/g," ") transforms the object into a JSON string pass, and the function is automatically transformed into an object after it is received (the principle is not known).
Procedure explanation: Json.stringify () converts the object into a JSON string; replace (/\ "/g," ") converts the double quotation marks in the JSON string into single quotes, otherwise it will report an Unexpected end of input
error (this error is due to the JSON string that was taken.) Its double quotation marks conflict with the double quotes of the onclick control)
Code content is as follows
var $row = JSON.stringify(row).replace(/\"/g,"‘");//row的是一个对象 <a href="#" onclick="editParentRow(‘+$row+‘)">编辑</a>//拼接传递对象
function editParentRow(obj){ console.log(obj);//console打印 alert(obj);//提示框 }
JavaScript onclick passes an object parameter (when Easyui passes a row of data) Error: uncaught syntaxerror:unexpected identifier