基本上,我想在用户单击对话框中的按钮时从我的web应用程序中注销用户。尝试搜索并尝试以下代码,但在单击该按钮时都不起作用。我使用sweetalert.js显示对话框,并使用以下代码放置一个html按钮
<form action='/logout' method='post' id='logout_form'>
<a onclick='document.getElementById('logout_form').submit();' style='cursor: pointer;'>Log Out</a>
</form>插入到对话框主体中,但是按钮将不起作用,相反,我在javascript中得到一个Uncaught SyntaxError: Unexpected token }错误。以下是我可爱的提醒代码:
swal({
title: "Logout? Any data you've entered will not be saved.",
text: "<form action='/logout' method='post' id='logout_form'><a onclick='document.getElementById('logout_form').submit();' style='cursor: pointer;'>Log Out</a></form>",
html: true,
type: "warning",
showCancelButton: "No",
confirmButtonColor: "#D9534F",
confirmButtonText: "Yes, cancel it!",
closeOnConfirm: false
});会出什么问题呢?提前谢谢。
发布于 2017-07-20 21:34:18
您的引号和双引号有问题您的锚点定义无效,因为logout_form之前的单引号会终止字符串。
虽然用双引号括起来,但这不是问题,因为只有双引号才会标记字符串的边界:
{ text: "<form action='/logout' method='post' id='logout_form'><a onclick='document.getElementById('logout_form').submit();' style='cursor: pointer;'>Log Out</a></form>" }这意味着,您应该转义/屏蔽您的引号,以防止解析器抛出错误并错误地解释以下javascript:
text: "<form action='/logout' method='post' id='logout_form'><a onclick='document.getElementById(\'logout_form\').submit();' style='cursor: pointer;'>Log Out</a></form>",除非你还没有发布代码中的任何其他语法错误,这应该可以解决你的问题。
https://stackoverflow.com/questions/45214253
复制相似问题