在HTML中显示Javascript变量

时间:2017-03-19 12:35:53

标签: javascript html node.js

我在代码片段后指定了以下问题。

app.js

app.route('/conn').post(function (req,res,next) {

        var sys =require('util');
        var myPythonScript ="script1.py";
        var path =resolve("C:\\Python27\\python.exe");
        var pythonExecutable = path;

        var uint8arrayToString = function(data){
        return String.fromCharCode.apply(null, data);
        };

        const spawn = require('child_process').spawn;
        const scriptExecution = spawn(pythonExecutable, [myPythonScript]);

    // Handle normal output
    var chunk =' ';
    scriptExecution.stdout.on('data', function(data) {
        chunk +=data;
    });
    scriptExecution.stdout.on('data', function(data) {
        console.log(chunk);
    });

    // Handle error output
    scriptExecution.stderr.on('data', (data) => {
        // As said before, convert the Uint8Array to a readable string.
        console.log(uint8arrayToString(data));
    });

    scriptExecution.on('exit', (code) => {
        //console.log("Process quit with code : " + code);

    });
    res.write('<h2>chunk</h2>');
    // End data write
    //scriptExecution.stdin.end();
    });

upload.html

<html>
<form action ="/file" method ="post">

    MAC Address:<br>
  <input type="text" name="macadd" id="macadd"><br>

  Percentage:<br>
  <input type="text" name="percent" id="percent"><br>


<input type="submit"  value='Submit' id="upload">
<br>
<input type="reset" value="Reset">
</form>
<input type=button onClick="location.href='/home'" value='Home'><br>
<form action ="/conn" method= "post">
<input type="submit" value='Show Users'><br>
</form>

</html>

script1.py

import sys,time

def main():
    t=0
    while t!=5:
        t =t+1
        print t


# Start process
if __name__ == '__main__':
    main()

t的值存储在&#39;&#39;变量并通过console.log()我能够在控制台上看到t的值。我想在upload.html页面上显示这些值。我该怎么做呢?我尝试使用res.render和res.write函数,但没有显示任何内容。有什么办法吗?

2 个答案:

答案 0 :(得分:0)

好吧,如果你想在JS中用HTML显示一个变量,那么有一种简单的方法可以做到这一点。首先,您可以创建元素:https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

接下来,编辑创建的元素。在之前的步骤中,只需给它一个id(在上面和下面的网站上显示)。 https://www.w3schools.com/js/js_htmldom_html.asp

希望有效, Ben A.K.A BlackSky

答案 1 :(得分:0)

我使用以下代码显示值。

<form>