我有一个在谷歌工作表的Web应用程序中的一个HTML文件,我有2个必填字段,但用户仍然可以提交表单,而不是用"获取位置"按钮填充这些字段,即使它们是必填字段.这些字段是纬度和经度.

我试着go 掉占位符,也试着go 掉样式,但到目前为止什么都没起作用.

这里有一个指向表格的链接和代码:Sheet

以下是HTML代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-16">
    <title>Google Sheet Form</title>
    <style>
      form {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1rem;
      }

      label {
        font-weight: bold;
      }

      input[type="text"],
      input[type="email"] {
        padding: 0.5rem;
        border: 2px solid gray;
        border-radius: 0.5rem;
        font-size: 2rem;
        width: 100%;
      }

      /* Adjust Get Location button size */
    button[type="button"] {
      padding: 0.5rem;
      border: none;
      border-radius: 0.5rem;
      font-size: 5rem; /* Change this value to adjust the font size */
      background-color: green;
      color: white;
      cursor: pointer;
    }

      button[type="submit"] {
        padding: 0.5rem;
        border: none;
        border-radius: 0.5rem;
        font-size: 5rem;
        background-color: blue;
        color: white;
        cursor: pointer;
      }

      button[type="submit"]:hover {
        background-color: darkblue;
      }
    </style>

    

    <script>
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
        alert("Geolocation is not supported by this browser.");
      }
    }

    function showPosition(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      document.getElementById("Latitude").value = latitude;
      document.getElementById("Longitude").value = longitude;
    }

    function showSuccessMessage() {
      const successMessage = document.getElementById('successMessage');
      successMessage.style.display = 'block';
    }

    // I added this function.
    function sample1(e) {
      google.script.run.withFailureHandler(err => console.log(err))
      .withSuccessHandler(f => {
        const responseText = '{"result": "success"}';
        const response = JSON.parse(responseText);
        if (response.result === 'success') {
          showSuccessMessage();
        }
      }).sample2(e);
    }
    </script>
  </head>
<body>
  <form>
    <span>Logged In: <?= email ?></span>
    <p> <input size="20" name="Email" type="email" placeholder="Email" required value="<?= email ?>" readonly> </p>
    <p> <input size="20" type="text" id="Latitude" name="Latitude" required readonly> </p>
    <p> <input size="20" type="text" id="Longitude" name="Longitude" placeholder="Longitude" required readonly> </p>
    <p> <button size="20" type="button" onclick="getLocation()">Get Location</button> </p>
    <p> <button size="20" type="submit" onclick="sample1(this.parentNode.parentNode); return false;">Sign In/Sign Out</button>
    </p>

    <!-- Add a div to display the success message -->
    <div id="successMessage" style="display: none;">Form submitted successfully!</div>

    <!-- Add a div to display the error message -->
    <div id="errorMessage" style="display: none;">An error occurred. Please try again.</div>
  </form>
</body>
</html>

我试着go 掉占位符,也试着go 掉样式,但到目前为止什么都没起作用.

推荐答案

我相信你的目标如下.

  • 如果未输入纬度和/或经度,则不希望提交这些值.

在这种情况下,判断纬度和经度的值如何?请按如下方式修改Java脚本的函数sample1.

出发地:

function sample1(e) {
  google.script.run.withFailureHandler(err => console.log(err))
  .withSuccessHandler(f => {
    const responseText = '{"result": "success"}';
    const response = JSON.parse(responseText);
    if (response.result === 'success') {
      showSuccessMessage();
    }
  }).sample2(e);
}

致:

function sample1(e) {
  const latitude = document.getElementById("Latitude").value;
  const longitude = document.getElementById("Longitude").value;
  if ([latitude, longitude].includes("")) {
    alert("Latitude and/or Longitude are not inputted.");
  } else {
    google.script.run.withFailureHandler(err => console.log(err))
    .withSuccessHandler(f => {
      const responseText = '{"result": "success"}';
      const response = JSON.parse(responseText);
      if (response.result === 'success') {
        showSuccessMessage();
      }
    }).sample2(e);
  }
}
  • 在此修改中,当未输入纬度和/或经度时,将打开alert 对话框并且不运行提交.
  • 如果您不想打开alert 对话框,请删除alert("Latitude and/or Longitude are not inputted.");.

Html相关问答推荐

如何让一个动态列表以网格方式水平显示

你能让Cypress测试为p—fileUpload做文件上传吗?

如何仅 Select 与子代CSS类匹配的第一个元素

在CSS中旋转排除的蒙版图像

在将DevExtreme升级到版本23.2之后,到处都会生成一个Pesudo类,我在其中使用了<;DXi-Item Title=&Quot;&Quot;>;

仅向上扩展并以最大高度滚动的Div

Angular -表和源映射中未显示的数据错误

CSS Grid:使网格行填充屏幕,将下一行推离屏幕

如何使用css横向显示英文和中日韩文字?

动画的停止和启动并不顺利

Flex:第一个 div 有列,下一个 div 有行

配置了 HTML 视口的 Google Apps 脚本移动网络应用程序无法正确显示

Swift WKUserContentController 和 WKScriptMessageHandler 设置不正确

为什么我的网页显示网格区域彼此相邻,而不是我设置它们的方式?

在 html 和 css 中绘制表格内的倾斜线

为什么当我按第三个按钮时,前两个按钮会跳动?

在DIV上应用梯度模糊未如预期工作

如何使用CSS Select 同级元素的::before伪元素?

当我将鼠标悬停在测试类上时,左侧类的 colored颜色 没有改变

Tailwind CSS 复选框样式不起作用