javascript - 为什么我点击 div 元素后会被重定向?

标签 javascript jquery html google-apps-script

我在 Google 表格背面构建了一个表单,并在其中一个 div 上创建了一个点击事件。由于某种原因,当我单击 div 时,它会正确执行,显示该部分内容,但立即将我重定向到另一个 URL。知道为什么吗?

Google 脚本:

function doGet(e) {
 return HtmlService
 .createTemplateFromFile('index')
 .evaluate()
 .setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

function writeForm(form) {
  try {  

      var companyName = form.companyName; //these match to the named 
      fields in your form

      var ss = SpreadsheetApp.openById('126iD9i- 
      KH_aV2WDUcwpUB52CS43rMxXklQjYz4HgPjM'); //the ID of the 
      spreadsheet you want to write to

      var sheet = ss.getActiveSheet();
      var newRow = sheet.getLastRow()+1;//go to the first blank row

      var range = sheet.getRange(newRow, 1);    
      range.setValue(companyName);

  } catch (error) {

      return error.toString();
  }
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <meta charset="utf-8">

    <!-- Mobile First -->
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Styles -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet">

    <!-- Scripts --> 
    <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

     <script>
        $(document).ready(function() {
            $('#accordion1').hover(function() {
                if($('#panel1').css("display") === 'none'){
                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_red.png");
                    $('#accordion1').css("background", "#ac0002");
                    $('#acc1Header').css("color", "#fff");
                }
                else{

                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_red_up.png");
                    $('#accordion1').css("background", "#ac0002");
                    $('#acc1Header').css("color", "#fff");

                }
            }, function() {
                if($('#panel1').css("display") === 'none'){
                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_gray.png");
                    $('#accordion1').css("background", "#eeeeee");
                    $('#acc1Header').css("color", "#545454");
                }
                else{

                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_red_up.png");
                    $('#accordion1').css("background", "#ac0002");
                    $('#acc1Header').css("color", "#fff");
                }
            });
        });
    </script>

    <style>
        .container-fluid {
            padding-right: 0;
            padding-left: 0;
            margin-right: auto;
            margin-left: auto;
        }

        body,
        html {
            background-color: white;
            margin: 0;
            padding: 0;
        }

        h1 {
            text-align: center;
        }

        h2 {
            text-align: center;
        }

        table {
            border: solid 1px black;
            border-collapse: collapse;
            width: 100%;
        }

        th,
        tr td {
            border: solid 1px black;
            padding: 5px;
        }

        hr {
            border-color: black;
        }

        .select-menu {
            width: 25%;
            height: 30px;
            border: 1px solid #ccc;
            border-radius: 2px;
            background-color: #ded9db;
        }

        legend {
            border-bottom: 1px solid black;
        }

        .btn span.glyphicon {
            opacity: 0;
        }

        .btn.active span.glyphicon {
            opacity: 1;
        }

        .tableCheck {
            text-align: center;
            vertical-align: middle;
        }

        .fixed-dialog {
            position: fixed;
            top: 50px;
            left: 50px;
        }

        .plusBtn {
            background-color: #edecde;
            border: none;
        }

        #tableDiv {
            overflow-x: auto;
        }

        #tableDiv2 {
            overflow-x: auto;
        }

        #tableDiv3 {
            overflow-x: auto;
        }

        #radioBtn .notActive {
            color: #000000;
            background-color: #fff;
        }

        #filePop1 {
            width: 25%;
            display: inline;
        }

        #filePop2 {
            width: 25%;
            display: inline;
        }

        #filepop3 {
            width: 25%;
            display: inline;
        }

        #confirmation p {
            font-size: 20px;
            font-style: oblique;
            width: 80%;
            margin: 0 auto;
        }

        #heading {
            text-align: center;
        }

        #divHead {
            height: 191px;
            width: 100%;
            margin: 0 !important;
            padding: 0 !important;
            background-image: url('http://smart-hr.com/img/DataGather_header.jpg');
            background-repeat: no-repeat;
            text-align: center;
            white-space: nowrap;
            -moz-box-shadow: 0 20px 20px -20px #000000;
            -webkit-box-shadow: 0 20px 20px -20px #000000;
            box-shadow: 0 20px 20px -20px #000000;
            position: relative;
            z-index: 10;
        }

        #grayHead {
            height: 95px;
            width: 100%;
            margin-top: 0 !important;
            margin-right: 0 !important;
            margin-left: 0 !important;
            margin-bottom: 5px !important;
            padding: 0 !important;
            background-image: url('http://smart-hr.com/img/DataGather_gray_header.jpg');
            background-repeat: no-repeat;
            text-align: center;
            white-space: nowrap;
            position: relative;
            z-index: 0;
        }

        .helper {
            display: inline-block;
            height: 100%;
            vertical-align: middle;
        }

        .headImg {
            vertical-align: middle;
        }

        #dataGather {
            font-family: 'Open Sans', sans-serif;
            font-weight: 600;
            font-size: 48px;
            color: #b5242b;
            vertical-align: middle;
            margin: 0px;
            display: inline-block;
        }

        .subButton{

            width: 12em;
            height: 3.5em;
            background-color: #d5d4d4;
            color: white;
            border: 0 none;
            font-family: 'Open Sans', sans-serif;
            font-weight: 600;
            font-size: 14px;
            text-transform: uppercase;

        }

        #subDiv{

            text-align: center;

        }
        button.accordion {
            background-color: #eee;
            color: #444;
            cursor: pointer;
            padding: 18px;
            width: 100%;
            border: none;
            text-align: center;
            outline: none;
            transition: 0.4s;
        }

        .accordion h1 {
            font-family: 'Open Sans', sans-serif;
            font-weight: 300;
            font-size: 55px;
            margin-top: 8px;
            padding-bottom: 50px;
            text-align: center;
            z-index: 2;
            position: relative;
            color: #545454;
        }

        button.accordion.active,
        button.accordion:hover {
            background-color: #ac0002;
        }

        button.accordion.active h1 {
            color: #fff;
        }

        #panel1 {
            background-color: white;
            display: none;
            color: black;
            font-size: 12px;
        }

        #panel2 {
            background-color: white;
            display: none;
            color: black;
            font-size: 12px;
        }

        #panel3 {
            background-color: white;
            display: none;
            color: black;
            font-size: 12px;
        }

        #panel4 {
            background-color: white;
            display: none;
            color: black;
            font-size: 12px;
        }

        #panel5 {
            background-color: white;
            display: none;
            color: black;
            font-size: 12px;
        }

    </style>
  </head>
  <body>
    <form class="form-horizontal" id="form" name="form" onsubmit="google.script.run.withSuccessHandler(formSubmitted) .writeForm(this); return false;">

        <button class="accordion" id="accordion1" name="accordion1">
                <h1 id="acc1Header">Company Basics</h1>
                <img id="acc1Button" src ="http://smart-hr.com/img/DataGather_circle_btn_gray.png">
            </button>
        <div class="panel" id="panel1">
            <br>
            <fieldset>
                <br>
                <br>
                <hr>
                <br>
                <br>
                <div class="form-group" id="group1">
                    <label class="control-label col-sm-4" for="companyName">1. Company Name:</label>
                    <div class="col-sm-5">
                        <input type="text" name="companyName" class="form-control" id="companyName">
                    </div>
                </div><br>
                </fieldset>
        </div>
        <script>
        //Script for clicking each section

        function clicked1() {
                console.log("clicked");
                if (!($('#panel1').css("display") == 'none')) {
                    console.log("not showing");
                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_red.png");
                    $('#panel1').hide();


                } else {
                    console.log("showing");
                    $('#acc1Button').attr("src", "http://smart-hr.com/img/DataGather_circle_btn_red_up.png");
                    $('#panel1').show();


                }


            }
            document.getElementById("accordion1").addEventListener("click", clicked1);
        </script>
        <!--<div id="subDiv">
            <input class="subButton" type="submit" onclick="return validateForm()">
        </div>-->
    </form>
  </body>
</html>

对于大量的代码,我深表歉意,并感谢任何人花时间查看它,请告诉我这里是否太多,或者您需要特定的信息而不是所有代码。非常感谢任何帮助。

最佳答案

如果我理解正确的话,问题与此按钮有关:

<button class="accordion" id="accordion1" name="accordion1">
  <h1 id="acc1Header">Company Basics</h1>
  <img id="acc1Button" src ="http://smart-hr.com/img/DataGather_circle_btn_gray.png">
</button>

这里您没有设置 type 属性,因此它将继承 W3C 规范的默认属性“提交”,并且由于您位于表单内,因此它将导致刷新,从而阻止您看到面板(即开头处被隐藏)。

尝试将类型设置为“button”,这将覆盖默认类型并应阻止您的代码访问服务器

<button class="accordion" type="button" id="accordion1" name="accordion1">
  <h1 id="acc1Header">Company Basics</h1>
  <img id="acc1Button" src ="http://smart-hr.com/img/DataGather_circle_btn_gray.png">
</button>

关于javascript - 为什么我点击 div 元素后会被重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375934/

相关文章:

javascript - angularjs:检查 ng-repeat 中过滤器的长度

php - 在与表单相同的模式窗口中加载 php 成功消息

javascript - 获取所选文本在 Div 中的正确位置

javascript - getElementById + 变量控制另一个 getElementById + 变量

javascript - 如何根据对象的时间属性显示一定时间范围内的对象?

javascript - 用点填充两个文本元素之间的空间

javascript - Coda:在页面重新加载时自动清除 JavaScript 控制台?

javascript - 触发文本框中文本的更改

Jquery draggable() 阻止 CSS resize 工作

php - 为什么 jquery ajax 会转到 url 而不是加载它?