我如何在输入字段中显示结果值?

时间:2018-09-03 07:34:33

标签: javascript html css

//这里我想使用javascript将温度从摄氏温度转换为华氏温度。当我输入摄氏温度值时,它会进行计算并可以使用alert显示结果。但是我想在法氏温度输入字段中显示结果。我该怎么办?

//var celcius = document.getElementById("celcius")
//var farenheit = document.getElementById("farenheit")
function converter() {
  farenheit.value = (celcius.value * 1.8) + 32;

  alert(farenheit.value);
  document.getElementById("farenheit") = farenheit.value;
}
.container {
  width: 500px;
  height: 500px;
  background-color: blue;
  color: white;
  margin: auto;
}

h1 {
  text-align: center;
}

.btn1 {
  text-align: center;
}

.celcius {
  text-align: center;
}

.farenheit {
  text-align: center;
}
<div class="container">
  <form>
    <h1>Temperature Converter</h1>
    <div class="celcius">
      <label>Celcius:</label>
      <input type="number" id="celcius" placeholder="Enter Temperature In Celcius">
    </div>
    <br><br><br>
    <div class="btn1">
      <button onclick="converter()">Converter</button>
    </div>
    <br><br><br>
    <div class="farenheit">
      <label>Farenheit:</label>
      <input type="number" id="farenheit" disabled>
    </div>
  </form>

</div>

5 个答案:

答案 0 :(得分:0)

直接使用farenheit.value

function converter(event) {

var celcius = document.getElementById("celcius")
var farenheit = document.getElementById("farenheit")
farenheit.value = (celcius.value * 1.8) + 32;

}
.container {
  width: 500px;
  height: 500px;
  background-color: blue;
  color: white;
  margin: auto;
}

h1 {
  text-align: center;
}

.btn1 {
  text-align: center;
}

.celcius {
  text-align: center;
}

.farenheit {
  text-align: center;
}
<div class="container">
  <form>
    <h1>Temperature Converter</h1>
    <div class="celcius">
      <label>Celcius:</label>
      <input type="number" id="celcius" placeholder="Enter Temperature In Celcius">
    </div>
    <br><br><br>
    <div class="btn1">
      <button type="button" onclick="converter()">Converter</button>
    </div>
    <br><br><br>
    <div class="farenheit">
      <label>Farenheit:</label>
      <input type="number" id="farenheit" disabled>
    </div>
  </form>

</div>

答案 1 :(得分:0)

根据所说的here,您可以使用document.getElementById("farenheit").value

答案 2 :(得分:0)

在HTML on button set type="button"中,JS代码应为

function converter() {

   var farenheit = (document.getElementById("celsius") * 1.8) + 32;

    document.getElementById("farenheit").value = farenheit;

  }

答案 3 :(得分:-1)

将您的JavaScript更改为此:

//var celcius = document.getElementById("celcius")
//var farenheit = document.getElementById("farenheit")
function converter() {
  farenheit.value = (celcius.value * 1.8) + 32;

  alert(farenheit.value);
  document.getElementById("farenheit").value = farenheit.value;
}

答案 4 :(得分:-1)

您可以使用它。

response.bodyBytes