首页 > 解决方案 > 如何使用按钮从 HTML 中的 JavaScript 调用函数?

问题描述

我正在为我的网站构建一个简单的插值和外推元素,用户会看到一个他用数值填写的表格,并且评估是在我的脚本中完成的。这是错误发出的区域,如有必要,我可以分享完整的代码。#JavaScrit 函数:

function Evaluate(){
if (xp > x0 && xp < x1) {
    console.log(Interp(x0, y0, x1, y1, xp))
} else if (xp > x0 && xp > x1) {
    console.log(Xtrap(x0, y0, x1, y1, xp))
}

#HTML 按钮在表单中:

<button type="button" id="button" onclick="Evaluate()">Evaluate</button>

#来自浏览器控制台查看器的错误(第 27 行是按钮):

Uncaught ReferenceError: Evaluate is not defined
    at HTMLButtonElement.onclick (index.html:27)

标签: javascripthtmlcss

解决方案


你犯了一个小错误你没有结束你的功能:

function Evalute() {
  if (xp > x0 && xp < x1) {
    console.log(Interp(x0, y0, x1, y1, xp))
  } else if (xp > x0 && xp > x1) {
    console.log(Xtrap(x0, y0, x1, y1, xp))
  }
}

推荐阅读