如何使用HTML按钮满足JavaScript门锁需求?

huangapple go评论55阅读模式
英文:

How to fulfill a JavaScript door unlock requirement using an HTML button?

问题

In this tutorial, you want to replace the checkbox with a button to control a door lock. Here's the modified code with the button:

<!DOCTYPE html>
<html>
<body>

<h1>Control Door Lock</h1>
<p><input type="button" id="lock" value="Lock/Unlock"></p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> <!-- include socket.io client side script -->
<script>
var socket = io(); // load socket.io-client and connect to the host that serves the page
window.addEventListener("load", function() { // when page loads
  var lockButton = document.getElementById("lock");
  var doorLocked = true; // Default position is locked
  
  lockButton.addEventListener("mousedown", function() { // add event listener for mouse button press
    doorLocked = false; // Unlock the door when the button is pressed
    socket.emit("lock", doorLocked); // send door status to server (as true or false)
  });
  
  lockButton.addEventListener("mouseup", function() { // add event listener for mouse button release
    doorLocked = true; // Lock the door when the button is released
    socket.emit("lock", doorLocked); // send door status to server (as true or false)
  });
  
  socket.on("lock", function(data) { // get door status from server
    doorLocked = data; // change button state according to door status
  });
  
  // Handle initial state (you may want to fetch the initial state from the server)
  lockButton.value = doorLocked ? "Locked" : "Unlocked";
});
</script>

</body>
</html>

This code replaces the checkbox with a button that toggles the door lock when pressed and released. The button initially displays "Locked" and changes to "Unlocked" when the door is unlocked, and vice versa.

英文:

In this this tutorial, https://www.w3schools.com/nodejs/nodejs_raspberrypi_webserver_websocket.asp , It has even handler that works off of an HTML checkbox. I would like to do the exact same thing, but with a button, so that I can temporarily unlock a door. As long as the button is pressed by the mouse, the door is unlocked. Once the mouse button is released, the door locks. The default position is locked. Below is the code from the link above:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;

&lt;h1&gt;Control LED light&lt;/h1&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; id=&quot;light&quot;&gt;&lt;/p&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js&quot;&gt;&lt;/script&gt; &lt;!-- include socket.io client side script --&gt;
&lt;script&gt;
var socket = io(); //load socket.io-client and connect to the host that serves the page
window.addEventListener(&quot;load&quot;, function(){ //when page loads
  var lightbox = document.getElementById(&quot;light&quot;);
  lightbox.addEventListener(&quot;change&quot;, function() { //add event listener for when checkbox changes
    socket.emit(&quot;light&quot;, Number(this.checked)); //send button status to server (as 1 or 0)
  });
});
socket.on(&#39;light&#39;, function (data) { //get button status from client
  document.getElementById(&quot;light&quot;).checked = data; //change checkbox according to push button on Raspberry Pi
  socket.emit(&quot;light&quot;, data); //send push button status to back to server
});
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt; 

Essentially, I would like to use

&lt;input type=&quot;button&quot; id=&quot;light&quot; value=&quot;light&quot;&gt;

instead of

&lt;input type=&quot;checkbox&quot; id=&quot;light&quot;&gt;

Thank you!

答案1

得分: 1

Sure, here is the translated code part:

使用数据属性在按钮上:

<input type="button" id="light" value="light" data-checked="0">

<!-- 语言: lang-js -->
window.addEventListener("load", function(){ // 页面加载时
  const lightButton = document.getElementById("light");
  lightButton.addEventListener("mousedown", function() { // 添加事件监听器以响应复选框更改
    socket.emit("light", Number(this.dataset.checked)); // 将按钮状态发送到服务器(作为1或0)
  });
});
socket.on('light', function (data) { // 从客户端获取按钮状态
  lightButton.dataset.checked = data; // 根据树莓派上的推按钮更改复选框
  socket.emit("light", data); // 将推按钮状态发送回服务器
});

<!-- 结束片段 -->

Please note that I've provided the translation for the code part as requested.

英文:

Use a data attribute on a button

&lt;input type=&quot;button&quot; id=&quot;light&quot; value=&quot;light&quot; data-checked=&quot;0&quot;&gt;

<!-- language: lang-js -->

window.addEventListener(&quot;load&quot;, function(){ //when page loads
  const lightButton = document.getElementById(&quot;light&quot;);
  lightButton.addEventListener(&quot;mousedown&quot;, function() { //add event listener for when checkbox changes
    socket.emit(&quot;light&quot;, Number(this.dataset.checked)); //send button status to server (as 1 or 0)
  });
});
socket.on(&#39;light&#39;, function (data) { //get button status from client
  lightButton.dataset.checked = data; //change checkbox according to push button on Raspberry Pi
  socket.emit(&quot;light&quot;, data); //send push button status to back to server
});

<!-- end snippet -->

答案2

得分: 1

为了使代码按我想要的方式正常工作,我进行了以下操作:

<input type="button" id="light" value="light" data-checked="1" data-unchecked="0">

然后我添加了以下JavaScript代码:

var socket = io();
window.addEventListener("load", function() { //页面加载时
    const lightButton = document.getElementById("light");
    
    lightButton.addEventListener("mousedown", function() { //添加事件监听器以侦听复选框更改
        socket.emit("light", Number(this.dataset.checked)); //将按钮状态发送到服务器(作为1或0)
    });
    
    lightButton.addEventListener("mouseup", function() { //添加事件监听器以侦听复选框更改
        socket.emit("light", Number(this.dataset.unchecked)); //将按钮状态发送到服务器(作为1或0)
    });
});
socket.on('light', function(data) { //从客户端获取按钮状态
    lightButton.dataset.checked = data; //根据树莓派上的按钮更改复选框
    socket.emit("light", data); //将按钮状态发送回服务器
});

@mplungjan,再次感谢您!

英文:

To get the code to work exactly the way I wanted it, I did the following;

&lt;input type=&quot;button&quot; id=&quot;light&quot; value=&quot;light&quot; data-checked=&quot;1&quot; data-unchecked=&quot;0&quot;&gt;

And I added the following JavaScript:

var socket = io();
window.addEventListener(&quot;load&quot;, function(){ //when page loads
const lightButton = document.getElementById(&quot;light&quot;);
lightButton.addEventListener(&quot;mousedown&quot;, function() { //add event listener for when checkbox changes
socket.emit(&quot;light&quot;, Number(this.dataset.checked)); //send button status to server (as 1 or 0)
});
const lightButton = document.getElementById(&quot;light&quot;);
lightButton.addEventListener(&quot;mouseup&quot;, function() { //add event listener for when checkbox changes
socket.emit(&quot;light&quot;, Number(this.dataset.unchecked)); //send button status to server (as 1 or 0)
});
});
socket.on(&#39;light&#39;, function (data) { //get button status from client
lightButton.dataset.checked = data; //change checkbox according to push button on Raspberry Pi
socket.emit(&quot;light&quot;, data); //send push button status to back to server
});

@mplungjan Thanks again!

huangapple
  • 本文由 发表于 2023年5月15日 12:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定