每个人我都是JavaScript和Html的新手,我正在尝试以html形式连接用户输入并提供ID并提交,以便将命令提供给我的控制器以采取相应的行动。
这基本上是为端口1设置时间表,命令就像[NORMAL 122(STARTTIMESTOPTIME DAYSESSION)我还没有包含会话。
function Port1_sch_On_off(){
if(connected_flag==1){
client.subscribe("lazy/test");
message = new Paho.MQTT.Message("NORMAL 122 + document.getElementById('port1_timeday'.value)");
message.destinationName = "lazy/test";
client.send(message);
}
else {
console.log("not connected")
}
return false;
}
<tr height=40>
<td><style="text-align:middle">PORT1_SCH_ON_OFF</td>
<td width=40></td>
<td><form id="port1_timeday" oninput="port1_timeday.value = start_time.valueAsNumber start_day.valueAsNumber stop_time.valueAsNumber stop_day ">
<input name="start_time" id="start_time" type="time" min="00:00" max="24:00" required/>
<span class="hours"></span>
<label for="start_day">Day:</label>
<select name="start_day" id="start_day">
<option value="1">MON</option>
<option value="2">TUES</option>
<option value="3">WED</option>
<option value="4">THRUS</option>
<option value="5">FRI</option>
<option value="6">SAT</option>
<option value="0">SUN</option>
</select>
</label>
<input name="stop_time" id="stop_time" type="time" min="00:00" max="24:00" required/>
<label for="stop_day">Day:</label>
<select name="stop_day" id="stop_day">
<option value="1">MON</option>
<option value="2">TUES</option>
<option value="3">WED</option>
<option value="4">THRUS</option>
<option value="5">FRI</option>
<option value="6">SAT</option>
<option value="0">SUN</option>
</select>
</label>
<output name="port1_timeday" for="start_time start_day stop_time stop_day"></output></td>
<input type="button" onclick="Port1_sch_On_off()" value="Port1_timeday">
</form>
</tr>
</table>
我还必须为尚未开始的事件添加事件监听器。
答案 0 :(得分:1)
以下是在javascript中进行连接的两种主要方法:
使用+运算符:
message = new Paho.MQTT.Message("NORMAL 122 " + document.getElementById('port1_timeday'.value));
或
使用字符串插值:
message = new Paho.MQTT.Message(`NORMAL 122 + ${document.getElementById('port1_timeday'.value)}`);
我希望这能回答您的问题:D
修改1:
此函数将返回您的用户输入,例如“ STARTDAYSTARTTIMESTOPTIMESTOPDAY”,您可以对其进行修改以按任意顺序返回值:
function cancat_day_time() {
start_time = document.getElementById("start_time").value;
start_day = document.getElementById("start_day").value;
stop_time = document.getElementById("stop_time").value;
stop_day = document.getElementById("stop_day").value;
return `${start_day}${start_time}${stop_day}${stop_time}`;
}
然后可以像下面这样在函数Port1_sch_On_off()中使用它:
function Port1_sch_On_off() {
if (connected_flag == 1) {
client.subscribe("lazy/test");
message = new Paho.MQTT.Message("NORMAL 122 " + cancat_day_time());
message.destinationName = "lazy/test";
client.send(message);
} else {
console.log("not connected")
}
return false;
}
编辑2:
这是对我有用的HTML代码:
<tr height=40>
<td></td>
<td width=40></td>
<td>
<form id="port1_timeday" oninput="">
<input name="start_time" id="start_time" type="time" min="00:00" max="24:00" required/>
<span class="hours"></span>
<label for="start_day">Day:</label>
<select name="start_day" id="start_day">
<option value="1">MON</option>
<option value="2">TUES</option>
<option value="3">WED</option>
<option value="4">THRUS</option>
<option value="5">FRI</option>
<option value="6">SAT</option>
<option value="0">SUN</option>
</select>
</label>
<input name="stop_time" id="stop_time" type="time" min="00:00" max="24:00" required/>
<label for="stop_day">Day:</label>
<select name="stop_day" id="stop_day">
<option value="1">MON</option>
<option value="2">TUES</option>
<option value="3">WED</option>
<option value="4">THRUS</option>
<option value="5">FRI</option>
<option value="6">SAT</option>
<option value="0">SUN</option>
</select>
</label>
</td>
<input type="button" onclick="Port1_sch_On_off()" value="Port1_timeday">
</form>
</tr>