javascript - 为什么我在由 php 回显的按钮中的 onclick 函数不起作用?
问题描述
我不知道为什么这行不通。它可以输出一个按钮元素,但 onclick 功能根本不起作用。甚至没有弹出警告框。请帮忙。
<!DOCTYPE html>
<html>
<body>
<?php $name = "John";
echo "<button type='button' onclick='callYou('".$name."')'> click me </button>";
?>
<script>
function callYou(name) {
if(confirm("Are you sure?")) {
$greeting = "hello"+name;
echo $greeting;
} else {
echo "nothing";
}
}
</script>
</body>
</html>
解决方案
您的代码中有多个错误,请尝试:
<!DOCTYPE html>
<html>
<body>
<?php
$name = 'John';
echo '<button type="button" onclick="callYou(\'' . $name . '\')"> click me </button>';
?>
<script>
function callYou(name) {
if(confirm("Are you sure?")) {
var greeting = "hello" + name;
alert(greeting)
} else {
alert("nothing")
}
}
</script>
</body>
</html>
去阅读一些关于 PHP、JavaScript 语法的文章吧!
推荐阅读
- spring - Spring Batch 失败更新表
- c# - C#更改MS word文档(.docx)上字符的颜色
- android - 如何从与正则表达式匹配的 chrome 中的 URL 启动应用程序?
- r - R中的kmeans算法中使用了哪些列,因为没有指定列的参数?
- java - 我的spring java项目中日期小于10的问题
- regex - Notepad ++中如何将IP地址移动到行尾
- rest - 如何使用 Apache HC 使用流响应主体
- java - Word to PDF to Notes Document 使用 POI4Xpages api
- javascript - jsPDF 将 PDF 附加到邮件
- php - 登录系统在 pdo php 中无法正常工作