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

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

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:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>Control Door Lock</h1>
  5. <p><input type="button" id="lock" value="Lock/Unlock"></p>
  6. <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> <!-- include socket.io client side script -->
  7. <script>
  8. var socket = io(); // load socket.io-client and connect to the host that serves the page
  9. window.addEventListener("load", function() { // when page loads
  10. var lockButton = document.getElementById("lock");
  11. var doorLocked = true; // Default position is locked
  12. lockButton.addEventListener("mousedown", function() { // add event listener for mouse button press
  13. doorLocked = false; // Unlock the door when the button is pressed
  14. socket.emit("lock", doorLocked); // send door status to server (as true or false)
  15. });
  16. lockButton.addEventListener("mouseup", function() { // add event listener for mouse button release
  17. doorLocked = true; // Lock the door when the button is released
  18. socket.emit("lock", doorLocked); // send door status to server (as true or false)
  19. });
  20. socket.on("lock", function(data) { // get door status from server
  21. doorLocked = data; // change button state according to door status
  22. });
  23. // Handle initial state (you may want to fetch the initial state from the server)
  24. lockButton.value = doorLocked ? "Locked" : "Unlocked";
  25. });
  26. </script>
  27. </body>
  28. </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:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;body&gt;
  4. &lt;h1&gt;Control LED light&lt;/h1&gt;
  5. &lt;p&gt;&lt;input type=&quot;checkbox&quot; id=&quot;light&quot;&gt;&lt;/p&gt;
  6. &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;
  7. &lt;script&gt;
  8. var socket = io(); //load socket.io-client and connect to the host that serves the page
  9. window.addEventListener(&quot;load&quot;, function(){ //when page loads
  10. var lightbox = document.getElementById(&quot;light&quot;);
  11. lightbox.addEventListener(&quot;change&quot;, function() { //add event listener for when checkbox changes
  12. socket.emit(&quot;light&quot;, Number(this.checked)); //send button status to server (as 1 or 0)
  13. });
  14. });
  15. socket.on(&#39;light&#39;, function (data) { //get button status from client
  16. document.getElementById(&quot;light&quot;).checked = data; //change checkbox according to push button on Raspberry Pi
  17. socket.emit(&quot;light&quot;, data); //send push button status to back to server
  18. });
  19. &lt;/script&gt;
  20. &lt;/body&gt;
  21. &lt;/html&gt;

Essentially, I would like to use

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

instead of

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

Thank you!

答案1

得分: 1

Sure, here is the translated code part:

  1. 使用数据属性在按钮上:
  2. <input type="button" id="light" value="light" data-checked="0">
  3. <!-- 语言: lang-js -->
  4. window.addEventListener("load", function(){ // 页面加载时
  5. const lightButton = document.getElementById("light");
  6. lightButton.addEventListener("mousedown", function() { // 添加事件监听器以响应复选框更改
  7. socket.emit("light", Number(this.dataset.checked)); // 将按钮状态发送到服务器(作为1或0)
  8. });
  9. });
  10. socket.on('light', function (data) { // 从客户端获取按钮状态
  11. lightButton.dataset.checked = data; // 根据树莓派上的推按钮更改复选框
  12. socket.emit("light", data); // 将推按钮状态发送回服务器
  13. });
  14. <!-- 结束片段 -->

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

英文:

Use a data attribute on a button

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

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

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

<!-- end snippet -->

答案2

得分: 1

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

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

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

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

@mplungjan,再次感谢您!

英文:

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

  1. &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:

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

@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:

确定