简体   繁体   English

为什么这个html片段不会识别我的javascript函数

[英]Why won't this html segment recognice my javascript function

I am rendering a graph with a couple of input fields using a function in WordPress. 我使用WordPress中的函数渲染带有几个输入字段的图形。 The js file is definitely available to this section of the html because the graph is rendering. js文件肯定可用于html的这一部分,因为图形是渲染的。 It is included through the enqueue_script function in wp. 它通过wp中的enqueue_script函数包含在内。 However, when I try to set the onclick function (updateChart()), I get an error saying my function isn't defined. 但是,当我尝试设置onclick函数(updateChart())时,我收到一条错误,指出我的函数未定义。 Been at it for a while... Any ideas? 已经有一段时间......有什么想法吗?

I have tried importing the script directl into the head above the tags I am trying to call it in. 我已经尝试将脚本directl导入到我试图调用它的标记上方的头部。

        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);
    }

The js code should get the input fields and update the chart using them. js代码应获取输入字段并使用它们更新图表。

The syntax in the line of code from which you're calling the function is correct. 您调用该函数的代码行中的语法是正确的。

<button id ='simulate' onclick= 'updateChart()' >Simulate</button>

When issues like these arise for me I usually add in some console.log() 's into the code to see what is actually executing. 当出现类似这样的问题时,我通常会在代码中添加一些console.log()来查看实际执行的内容。

Firstly, you say that the JS file is being called properly but just to make sure add in a console.log into the JS file and see if it shows in the console window. 首先,你说JS文件被正确调用,但只是为了确保将一个console.log添加到JS文件中,看看它是否显示在控制台窗口中。

Secondly, create a very simple function like.. 其次,创建一个非常简单的功能,如..

function myFunction() {
  console.log("Hello");
}

then call this function in your view like.. 然后在你的视图中调用此函数,如..

<button id ='simulate' onclick= 'myFunction()' >Simulate</button>

If 'Hello' appears in the console window then the issue is with the updateChart() function and not the implementation. 如果控制台窗口中出现“Hello”,则问题在于updateChart()函数,而不是实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM