简体   繁体   English

单击div元素后为什么会被重定向?

[英]Why am I being redirected after clicking on my div element?

I have a form I'm building on the back of a Google Sheet, and have created a click event on one of the divs. 我有一个要构建在Google表格背面的表单,并且已在一个div上创建了一个click事件。 For some reason, when I click the div it executes properly, showing the section content but immediately redirects me to another URL. 出于某种原因,当我单击div时,它会正确执行,显示部分内容,但立即将我重定向到另一个URL。 Any idea why? 知道为什么吗?

Google Script: 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: 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>

I apologise for the extensive amount of code, and appreciate anyone taking the time to look at it, please tell me if there is too much here or you need a particular piece of information instead of all the code. 对于大量的代码,我深表歉意,同时感谢您抽出宝贵的时间来查看它,请告诉我这里是否有太多代码,或者您需要特定的信息而不是全部代码。 Any assistance is greatly appreciated. 非常感谢您的协助。

If i understood correctly the problem is related to this button: 如果我正确理解问题与该按钮有关:

<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>

Here you're not setting the type attribute, hence it will inherit the default one that for W3C specification is "submit", and since you're inside a form it will cause a refresh preventing you from seeing the panel (that at the beginning is hidden). 在这里,您无需设置type属性,因此它将继承W3C规范默认的“提交”属性,并且由于您位于表单内,因此将导致刷新,从而使您无法看到面板(在开始时隐藏)。

try just to set the type to "button" , this will override the default type and should prevent your code to hit the server 尝试仅将类型设置为“ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会被重定向到此联系表单中的 php 文件? - Why am I being redirected to my php file in this contact form? 为什么不将我重定向到新页面? - Why am I not being redirected to a new page? 为什么我没有在应该重定向的时候被重定向 - Why am I not being redirected when I should 为什么我无法恢复 div 元素的颜色? - Why am I not able to revive the colour of my div element? 为什么单击按钮时会附加多个元素? - Why i am getting multiple element appended while clicking the button? 重定向到首页网址后,如何使用Javascript / Jquery实现自动点击? - How can I achieve an autoclick with Javascript / Jquery after being redirected to my homepage url? 我正在为我的应用程序使用Firebase身份验证。 似乎有一个无限循环,页面不断被重定向 - I am using Firebase authentication for my application. There seems to be an infinite loop where the page keeps being redirected over and over again 单击此元素的任何子元素后,Div隐藏 - Div hides after clicking on any child of this element 我正在尝试排列我的html / css / jquery,因此我可以通过双击来切换div的可见性 - I am trying to arrange my html/css/jquery so I can toggle the visibility of a div by double clicking on it 为什么我没有重定向到我选择的路径? 反应路由器 - Why am I not being redirect to my selected path? React Router
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM