javascript - 如何使用 onclick 使用 PHP 调用 javascript 函数
问题描述
我需要将 PHP 中定义的变量传递给 javascript 函数。JS 函数需要通过按钮 onclick 事件调用。
<button type="button" style="width: 70px;" class="btn btn-light btn-sm Center"
onclick="<?php echo '<script>purchase('{$user}','{$painting1_Title}','200')</script>'?>">Purchase</button>
我尝试了单引号和双引号的不同含义,但出现解析错误:
解析错误:语法错误,意外的 '','' (T_CONSTANT_ENCAPSED_STRING),需要 ',' 或 ';' 在第 303 行的 /Applications/XAMPP/xamppfiles/htdocs/ProjectWebsite/Mainpage2.php
所以我的问题是如何在 onclick 事件中使用 PHP 调用 javascript 函数?
解决方案
您始终可以在其他引号内转义引号,以防止引号嵌套的噩梦。
"This is how you would \"fix\" certain \"quotation issues\""
我的看法是使用在双引号内转义的单引号,即使通常单引号在双引号内工作。
"This is how you would \'fix\' certain \'quotation issues\'"
推荐阅读
- web-scraping - 从未调用 RULE 中的 CrawlSpider 回调函数
- java - 为什么try中的int编译成byte
- python - pd.concat 中轴 = 1 和轴 = 0 之间差异不明确的异常情况
- python - Python Tkinter:在一个窗口上显示每个窗口
- java - 如何获取地图上当前显示的位置
- linux - linux中的Eclipse索引缓存位置
- deep-learning - PyTorch:如何根据每个 GPU 的性能分配训练工作?
- android - 我希望将网格视图作为一个整体显示
- java - 为什么数据没有显示在 adpaters android 中
- node.js - Angular 网站路径无法正常工作