我有一個html文件,它調用一個javascript函數從用戶獲取6位數的輸入。如何將javascript變量導出到節點j
<html>
<head>
<script src="ho.js"></script>
<h1>Sample</h1>
</head>
<body>
<input type="tel" id="realOTP" placeholder="Enter Your OTP" maxlength="6">
<input type="submit" value="Use OTP" onclick="compute()">
</body>
</html>
這是定義函數的ho.js javascript文件。
var a=10, arr=[];
var exports = module.exports = {};
function compute() {
rOTP = document.getElementById('realOTP').value;
if (rOTP.length == 0) {
alert('Enter a value');
return;
};
arr = String(rOTP).split("");
console.log("Entered OTP -> " + arr);
return arr;
}
exports.array = compute.arr;//what should come here?
exports.r = a;
console.log("a:" +a);
exports.fun =function(){
console.log("in function");
var mes = require("./ho").r;
console.log("mes:"+mes);
var mes2 = require("./ho").array;
console.log("mes2:"+mes2);
}
是否有可能給JavaScript函數(在這種情況下,「改編」)的返回值導出到節點JS出口功能類似於全局變量a = 10。這是我通過瀏覽器獲取「arr」的值後調用的主節點文件。
hoh.js
var call = require("./ho");
console.log("hoh:" +call.r);
call.fun();
這是當我運行hoh.js輸出我得到:
C:\Users\Balajee\Desktop\project\Ultro>node hoh.js
a:10
hoh:10
in function
mes:10
mes2:undefined
你可以嘗試'exports.array =計算()' – JordanHendrix
@JordanHendrix它看起來像計算()是指在他的HTML提交按鈕來調用。導出這樣的計算結果將不允許代碼考慮對id爲「realOTP」的輸入的修改。 – mjohnsonengr
我會收到DOM錯誤,因爲compute是一個在瀏覽器上運行的Javascript函數。當我將它分配給運行在瀏覽器上的節點時會拋出像C:\ Users \ Balajee \ Desktop \ project \ Ultro \ ho.js這樣的錯誤:4 rOTP = document.getElementById('realOTP').value; ^ ReferenceError:文檔沒有定義 –