javascript寫網頁的一個按鈕,點擊後發送給安卓APP一條信息,並讓按鈕不可再按。在接收安卓app的回信後激活按鈕
时间: 2023-12-18 21:03:25 浏览: 24
可以通过JavaScript的XMLHttpRequest对象来发送信息给安卓APP,同时在点击按钮时禁用按钮,接收到安卓APP的回信后再启用按钮。以下是可能的代码示例:
```html
<button id="myButton" onclick="sendInfo()">发送信息</button>
<script>
function sendInfo() {
// 禁用按钮
document.getElementById("myButton").disabled = true;
// 发送信息
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://yourapp.com/send-message", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// 接收到回信,启用按钮
document.getElementById("myButton").disabled = false;
}
};
xhr.send("message=hello");
}
</script>
```
在这个示例中,当按钮被点击时,JavaScript将发送一个带有"message=hello"参数的POST请求到"http://yourapp.com/send-message"。在发送请求时,按钮被禁用,等待回信。一旦收到回信,按钮将被启用。
请注意,这只是一个简单的示例,实际上你需要更多的代码来处理错误情况、处理回信等。