返回值没有显示在html中

Return value did not show up in html

本文关键字:html 显示 返回值      更新时间:2023-09-26

这是HTML代码:

<html>
<head>
<script src = "tempScript.js" text = "text/javascript"> 
</script>
</head>
<body>
    <h3><b>Farenheit to Celsius Converter</b></h3>
    <p>Enter a temperature in degrees F:&nbsp;
        <input type="text" id="textbox" /></p>
    <p>Click this button to calculate the temperature in degrees C:&nbsp;
        <input type="submit" value="Calculate" onclick="temp ()" /></p>
    <p>Temperature in degrees C is:&nbsp;<input type="temp ()"/></p>
</body>
</html>

这是javascript代码:

function temp ()
{
    var f = document.getElementById("textbox").value;
    var c = ((f - 32.0)*5.0) / 9.0;
    return c;
}

这些代码有什么问题吗?

实际上,我想做的是获得返回值,然后在"温度在摄氏度是"的字段中显示值。但是我找不到错误。如果我有错误,请指出来。

你不能在'value'中调用/assign方法下面是正确的方法

    <h3><b>Farenheit to Celsius Converter</b></h3>
<p>Enter a temperature in degrees F:&nbsp;
    <input type="text" id="textbox" /></p>
<p>Click this button to calculate the temperature in degrees C:&nbsp;
    <input type="submit" value="Calculate" onclick="temp();" /></p>
<p>Temperature in degrees C is:&nbsp;<input id="degrees" /></p>
  <script type="text/javascript">
    function temp() {
        var f = document.getElementById("textbox").value;
        var c = ((f - 32.0) * 5.0) / 9.0;
        document.getElementById("degrees").value = c;
    }
</script>

<input type="temp ()"/>是无效的html。试试这个。

<html>
<head>
<script src = "tempScript.js" text = "text/javascript"> 
</script>
</head>
<body>
    <h3><b>Farenheit to Celsius Converter</b></h3>
    <p>Enter a temperature in degrees F:&nbsp;
        <input type="text" id="textbox" /></p>
    <p>Click this button to calculate the temperature in degrees C:&nbsp;
        <input type="submit" value="Calculate" onclick="temp ()" /></p>
    <p>Temperature in degrees C is:&nbsp;<input id="tmpDisplay" value=""/></p>
</body>
</html>
<script>
    function temp ()
{
    var f = document.getElementById("textbox").value;
    var c = ((f - 32.0)*5.0) / 9.0;
    document.getElementById("tmpDisplay").value=c;
    return false;
}
</script>

注意:我在显示结果的input中添加了和id。将该输入的值设置为javascript中的计算值。注意脚本中的return false。这是为了防止表单被提交到服务器

我认为错误在行:

<input type="temp ()"/>

输入的类型和显示的格式。您需要添加一个返回值:

<input type="text" value="temp()"/>

然后在html中显示结果。

与其将其放在input的type()中,不如将其作为value。工作小提琴——

http://jsfiddle.net/RDue8/2/