douliao8318 2017-01-17 21:12
浏览 62

footer.php wordpress中的一段javascript可以在PC上运行,但不能在移动设备上运行

I have the following code in my footer.php on my wordpress website

var myForm = document.getElementById("questionnaire");
if (myForm) {
  myForm.onsubmit = function() {
var questionDiv = document.getElementById('question-div');
var busyDiv = document.getElementById('busy');
var resultDiv = document.getElementById('result-div');
var resultList = document.getElementById('result-list');
var xhr = new XMLHttpRequest();
var url = "https://script.google.com/macros/s/xxxxx/exec";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhr.onload = function() {
  if(xhr.readyState == 4 && xhr.status == 200) {
    var response = JSON.parse(xhr.responseText);
    var result = response.result;
    for (var i in result) {
      var e = result[i];
      if (e) {
        var li = document.createElement('li');
        if (e.url) {
          var a = document.createElement('a');
          a.href = e.url;
          a.textContent = e.name;
          li.appendChild(a);
        } else {
          li.textContent = e.name;
        }
        resultList.appendChild(li);
      }
    }
    busyDiv.hidden = true;
    resultDiv.hidden = false;
  }
}

var form = document.getElementById('questionnaire');
var formData = new FormData(form);
var fields = ['name','email','yob','gender','xxx','yyyy'];
var params = [];
for (var i in fields) {
  var field = fields[i];
  params.push(field + "=" + formData.get(field));
}
xhr.send(params.join('&'));

questionDiv.hidden=true;
busyDiv.hidden=false;


return false;
  };
}
</script>

This has allowed me to write and quote data from a google sheet

Now all this works on PC but it delivers 404 not found error on mobile site and the website itself is preset to be mobile responsive.

Can somebody help please?

Jane

  • 写回答

1条回答 默认 最新

  • doubian6241 2017-01-17 22:28
    关注

    What does your form html look like? I suspect that your mobile browser is trying to submit the form via the standard - non AJAX way and therefore is directing the browser to the url in the "action" attribute of your form. Try the following to stop this default behaviour

    var myForm = document.getElementById("questionnaire");
    if (myForm) {
      myForm.onsubmit = function(evt) {
        evt.preventDefault();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用