【问题标题】:use html button to call javascript function [closed]使用html按钮调用javascript函数[关闭]
【发布时间】:2018-07-30 02:08:19
【问题描述】:

当我写 a=1 b=-3 和 c=-4 以及按下按钮时,我需要将输入与三个数字连接起来 --> 以获得解决方案

function solve(a, b, c) {
    let x1 = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    let x2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return "X1= " + x1 + ", X2= " + x2;
}
console.log(solve(1, -3, -4));

【问题讨论】:

  • “看起来更完整”是什么意思?请解释您的问题,以便其他人可以帮助您。设身处地为我们着想——我们无法读懂你的想法
  • 添加三个<input>元素和一个<button>元素并调用按钮的onclick中的函数,传递三个input元素的值。请记住使用唯一 ID。

标签: javascript html


【解决方案1】:

我不完全理解您的问题,但我猜您希望您的 solve() 函数在按下按钮时执行。以下是三个选项:

window.onload = function() {

    var button = document.getElementById("button");

    // Method 1

    button.addEventListener("click", function() {
        alert("clicked1");
        solve(a, b, c);
        // ^ Insert Params ^ 
    });

    // Method 2

    button.onclick = function() {
        alert("clicked2");
        solve(a, b, c);
        // ^ Insert Params ^
    };
    
};
#button {
    width: 100px;
    height: 100px;
    background-color: green;
}
<!DOCTYPE html>
<html>
    <head>
    
    </head>
    <body>
        <div id="button"> <p>This is a button</p></div>
    </body>
</html>

我希望这能解决你的问题。

【讨论】:

  • 如果这是你要找的答案,那确实是@Juan 所说的重复。
  • 是的,这就是我还需要将输入与三个数字连接起来的一件事,当我写 a= b= 和 c= 并按下按钮时 --> 有一个解决方案
猜你喜欢
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
相关资源
最近更新 更多