无法获得选定的下拉值,在 验证输入 功能
下面的代码用于验证用户在文本框中输入的信息,并根据在下拉菜单中选择的数据类型提供响应。
HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML.JS: HTML:
<script type="text/javascript" src="input.js">
RED.nodes.registerType("myNode", {
category: "function",
color: "#a6bbcf",
defaults: {
data: {
value: ""
}
},
inputs: 1,
outputs: 1,
icon: "file.png",
label: function() {
return this.data || "myNode";
},
oneditprepare: function() {
$("#selectMe").on("click", function() {
var selectedVal = $(this).find(':selected').val();
console.log("selectedVal:", selectedVal)
if (selectedVal == "photograph") {
$("#select-text").hide();
$("#select-photograph").show();
} else if (selectedVal == "geolocation" || selectedVal == "text" || selectedVal == "number" || selectedVal == "decimal" || selectedVal == "sensor") {
// this.data = "MYDATA";
$("#select-photograph").hide();
$("#select-text").show();
} else {
$("#select-text").hide();
$("#select-photograph").hide();
}
})
},
});
</script>
<script type="text/html" data-template-name="myNode">
<div class="form-row">
<label for="node-select-data"><i class="icon-tag"></i> Choose Input type that you want:</label>
<select id="selectMe">
<option value="">Select Data Type</option>
<option value="text">String</option>
<option value="number">Integer</option>
<option value="decimal">Decimal</option>
<option value="sensor">Sensor</option>
<option value="geolocation">Geo Location</option>
<option value="photograph">Photograph</option>
</select>
</div>
<div id="select-text" hidden>
<label for="node-input-data"><i class="icon-tag"></i> Enter the Data to be validated:</label>
<input type="text" id="node-input-data" placeholder="Data..">
</div>
<div id="select-photograph" hidden>
<label for="node-input-data">Select a Photograph file:</label>
<input type="file" id="node-input-data" name="myfile">
</div>
</script>
<script type="text/html " data-help-name="myNode ">
<p>A simple node that validates the Data respect to the data type entered</p>
</script>
JS:
module.exports = function(RED) {
"use strict ";
function ValidateInput(config) {
RED.nodes.createNode(this, config);
this.log("RED:", RED);
this.data = config.data;
console.log("config:", config);
console.log("data:", data);
var node = this;
node.on("select", function(msg, send, done) {
var inputEntered = node.data;
});
node.on("input", function(msg, send, done) {
var inputEntered = node.data;
var letters = /^[A-Za-z]+$/;
var numbers = /^[0-9]+$/;
var decimal = /^[-+]?[0-9]+\.[0-9]+$/;
if (inputEntered.match(letters)) {
msg = {
payload: "Your input have been accepted",
};
} else if (inputEntered.match(numbers)) {
msg = {
payload: "Your input have been accepted",
};
} else if (inputEntered.match(decimal)) {
msg = {
payload: "Your input have been accepted",
};
} else {
msg = {
payload: "Your input is wrong for this field",
};
}
node.send(msg);
});
}
RED.nodes.registerType("myNode", ValidateInput);
};
你似乎对Node-RED节点的一般概念有误解。
HTML文件(和包含的脚本)在浏览器中运行,在配置时使用。
JS文件完全在后端运行时运行,并且不能直接访问HTML文件中任何没有被传递为 config
的节点。
如果你想验证配置输入,那么你需要在HTML文件的 oneditsave
处理程序而不是JS文件。
而不是JS文件。node.on('input',...)
函数是在之前有线节点发送新消息时调用的,它不是用来处理配置信息的。
我建议你阅读 文档 关于如何编写Node-RED节点。