我正在使用WordPress中的一个函数呈现一个包含两个输入字段的图形。js文件对于html的这一部分绝对是可用的,因为图形正在呈现。它是通过wp中的enqueue_script函数包含的。但是,当我尝试设置onclick函数(updateChart())时,我得到一个错误,指出我的函数没有定义。已经有一段时间了.有什么想法吗?
我已经尝试将脚本directl导入到我试图调用它的标签上方的head中。
ob_start();?>
<head>
<!–– All styling should take place below ––>
<!–– First div id must match the first argument of Chart Object in js file ––>
</head>
<body>
<div id='chartContainer' style='height: 300px; width: 100%;'></div>
<input type='number' id='voltage' placeholder='Voltage' step='0.01' />
<input type='number' id='amperage' placeholder='Amperage' step='0.01' />
<button id ='simulate' onclick= 'updateChart()' >Simulate</button>
</body>
<?php
$content = ob_get_clean();
echo($content);
}
js代码应该获取输入字段并使用它们更新图表。
发布于 2019-06-13 07:38:26
调用函数的代码行中的语法是正确的。
<button id ='simulate' onclick= 'updateChart()' >Simulate</button>
当我遇到这样的问题时,我通常会在代码中添加一些console.log()
,以查看实际执行的是什么。
首先,您说JS文件被正确调用,但只是为了确保将一个console.log
添加到JS文件中,并查看它是否显示在控制台窗口中。
其次,创建一个非常简单的函数,比如..
function myFunction() {
console.log("Hello");
}
然后在视图中调用此函数,如下所示..
<button id ='simulate' onclick= 'myFunction()' >Simulate</button>
如果控制台窗口中出现“Hello”,则问题出在updateChart()
函数而不是实现上。
https://stackoverflow.com/questions/56571430
复制相似问题