javascript 如何打印给定图像中第三个空白框内的输出[重复]

mpgws1up  于 2023-03-21  发布在  Java
关注(0)|答案(2)|浏览(81)

此问题在此处已有答案

How to prevent form from being submitted?(11个答案)
How to update an input text with JavaScript?(2个答案)
20小时前关门了。
我试图在第三个框中显示我的输出,但它在一个单独的页面中显示输出。如何解决这个问题。

function mul(){
    var a=parseInt(document.getElementById("f_num").value);
    var b=parseInt(document.getElementById("s_num").value);
    var res= a*b;
    document.write(res);
}
function div(){
    var a=parseInt(document.getElementById("f_num").value);
    var b=parseInt(document.getElementById("s_num").value);
    var res= a/b;
    document.write();
}
<form>
  <input type="text" id="f_num" placeholder="Enter your first number"><br><br>
  <input type="text" id="s_num" placeholder="Enter your second number"><br><br>
  <input type="submit" value="Multiply" onclick="mul()">
  <input type="submit" value="Divide" onclick="div()"><br><br>
  <input type="text">
</form>
lymnna71

lymnna711#

function mul() {
    var a = parseInt(document.getElementById("f_num").value);
    var b = parseInt(document.getElementById("s_num").value);
    var res = a * b;
    document.getElementById("result").value = res;
}

function div() {
    var a = parseInt(document.getElementById("f_num").value);
    var b = parseInt(document.getElementById("s_num").value);
    var res = a / b;
    document.getElementById("result").value = res;
}
<form>
  <input type="text" id="f_num" placeholder="Enter your first number"><br><br>
  <input type="text" id="s_num" placeholder="Enter your second number"><br><br>
  <input type="button" value="Multiply" onclick="mul()">
  <input type="button" value="Divide" onclick="div()"><br><br>
  <input type="text" id="result">
</form>

检查此代码解决您的问题...

weylhg0b

weylhg0b2#

您的问题由两部分组成:
您正在使用type="submit"form提交一个带有按钮的表单。这将把表单提交到一个新页面。您可能会丢失表单标签并将按钮更改为实际按钮。
你还将结果写入文档,但不是元素。在下面的代码片段中,我添加了将结果写入输入的代码。

function mul() {
  var a = parseInt(document.getElementById("f_num").value);
  var b = parseInt(document.getElementById("s_num").value);
  var res = a * b;
  document.getElementById("answer").value = res;
}

function div() {
  var a = parseInt(document.getElementById("f_num").value);
  var b = parseInt(document.getElementById("s_num").value);
  var res = a / b;
  document.getElementById("answer").value = res;
}
<input type="text" id="f_num" placeholder="Enter your first number"><br><br>
<input type="text" id="s_num" placeholder="Enter your second number"><br><br>
<button type="button" onclick="mul()">Multiply</button>
<button type="button" onclick="div()">Divide</button><br><br>
<input type="text" id="answer">

相关问题