如何用正确的值更新事物口中的按钮一次?

时间:2019-11-12 07:35:44

标签: javascript jquery ajax rest api

我似乎无法使按钮更新一次,它首先需要将正确的值作为更新。但是,当“关闭”按钮打开时,它会迅速将其更新为该值,而在我的频道中却没有。如何使它工作?这意味着我希望我的按钮与打开和关闭灯时的按钮一样工作。含义on必须更新一次并更新正确的值,而在打开时不要更新两次。与off相同,必须将其关闭,而不更新on按钮的值。

<!--HTML---code>
  <div class="form-group"><br>
  <div class="col-md-2 text-center">  
<button id="singlebutton" name="singlebutton" class="btn btn-danger" 
    onclick="BtnOff();">Off</button><br>   
</div> 
  </div> 
<br/> 
  <div class="form-group"><br>
<div class = "col-md-2 text-center">  
<button id = "singlebtn" name="singlebtn" class="btn btn-success" 
  onclick="BtnOn();">On</button><br>   
</div>
</div>

<!--Javascript-->
// button click functionality here.



        function BtnOff () {  
            $.ajax({  
                url: 'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=0',  
                type: 'GET',  
                data: {  
                    format: 'text'  
                },  
                success: function (response) {  
                    alert(response);  
                },  
                error: function () {  
                    $('#error').text("There was an error processing your request.Please try again");  
                    $('#singlebutton').append(data);  
                }  
            });  
        }  


         function BtnOn() {  

            $.ajax({  
                url: 'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=1',  
                type: 'GET',  
                data: {  
                    format: 'text'  
                },  
                success: function (response) {  
                    alert(response);  
                },  
                error: function () {  
                    $('#error').text("There was an error processing your request.Please try again");  
                    $('#singlebtn').append(data);  
                }  
            });  
        }

0 个答案:

没有答案