javascript - 使用 JQuery Mobile 按钮触发 Rest API 调用

标签 javascript jquery ajax jquery-mobile

这很简单,但我的 JS 有点生疏.. 我试图通过按 JQuery Mobile 元素来触发获取请求。 我可以看到按钮,但无法执行实际的获取请求

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>

<body>
<form>
    <label for="flip-checkbox-1">Flip toggle switch checkbox:</label>
    <p><input type="checkbox" data-role="flipswitch" name="flip-checkbox-1" id="generator_button"></p>
</form>
</body>

<script>
$("p").on("tap",function(){
  $.ajax({
    'url' : '192.168.8.110:5000/generator_on',
    'type' : 'GET',
    'success' : function(data) {
      if (data == "success") {
        alert('request sent!');
      }
    }
  });
</script>

最佳答案

请注意:在您的标记中有一个类型为checkboxFlip switch,而不是一个按钮,所以我相信您最好检查底层checkbox 而不是坚持 tap 事件。

引用:jQuery Mobile Flip switch

此外,您最好为所有内容提供一个 JQM 页面结构。

这是一个例子:

function sendRequest() {
  $.ajax({
    'url': '192.168.8.110:5000/generator_on',
    'type': 'GET',
    'success': function(data) {
      if (data == "success") {
        alert('request sent!');
      }
    }
  });
}

$(document).on("change", "#generator_button", function() {
  var state = $("#generator_button").prop("checked");
  if (state === true) {
    // Flip switch is on
    console.log("Request sent.");
    //sendRequest(); // uncomment
  } else {
    // Flip switch is off
    //...endpoint _off
  }
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
  </head>
<body>
  <div id="page-one" data-role="page">
    <div data-role="header" data-position="fixed">
      <h1>Header</h1>
    </div>
    <div role="main" class="ui-content">
      <label for="flip-checkbox-1">Flip toggle switch checkbox:</label>
      <input type="checkbox" data-role="flipswitch" name="generator_button" id="generator_button">
    </div>
    <div data-role="footer" data-position="fixed">
      <h1>Footer</h1>
    </div>
  </div>
</body>
</html>

关于javascript - 使用 JQuery Mobile 按钮触发 Rest API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45000563/

相关文章:

javascript - onclick 属性与脚本中的 onclick

javascript - 如何在有序列表之间插入新的 div?

javascript - 在页面标题中插入带有空格的播放图标 - 就像 YouTube

javascript - 给定一个数组;如何为第一个元素运行函数并有条件地等待、返回或运行下一个元素的函数?

javascript - 如何在不以 channel 所有者身份登录谷歌的情况下使用 Youtube Analytics API 从授权 channel 接收数据?

javascript - 添加图像链接

javascript - 使用jquery插件使来自不同表的两个td可排序

ajax - 使用 codeigniter 将状态从事件状态更新为非事件状态

javascript - AJAX请求错误: "SyntaxError: JSON.parse: unexpected character"

php - 使用 AJAX 初始化 jQuery Multiselect