根据单选按钮的选择切换子类别div [英] Toggle sub-category divs based on radio button selection

查看:64
本文介绍了根据单选按钮的选择切换子类别div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了此问题的最佳答案构建一个表单,该表单将与文件上传一起输入到工作表中.现在,我碰到了另一堵墙.

I used the top answer to this question to build a form that feeds into a sheet along with file upload. Now I've hit another wall.

我有类别和子类别.我希望子类别仅在选择了其父类别时才显示.我只是想不出A)我需要将代码放在哪里(在我们的网站上,它正好与HTML一起放置),我尝试将其放在HTML文件和Code.gs文件中,或者B)如果我正在使用的代码甚至正确.

I have categories, and sub-categories. I'd like the sub-categories to only show up IF their parent category has been selected. I just can't figure out A) where I need to put the code (on our website it's right in with the HTML), I've tried putting it in the HTML file and the Code.gs file, or B) if the code I'm using is even right.

此处为表格-合作社类别"是父类别,对于每个包含子类别"的类别,我都隐藏了divs

Here's the form - the "Co-Op Category" is the parent categories, I have hidden divs for each category that would hold the 'child categories'

HTML:

   <script>
  // Javascript function called by "submit" button handler,
  // to show results.
  function updateOutput(resultHtml) {
    toggle_visibility('inProgress');
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = resultHtml;
  }

  // From blog.movalog.com/a/javascript-toggle-visibility/
  function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
      e.style.display = 'none';
    else
      e.style.display = 'block';
  }
</script>

<div id="formDiv">
<!-- Form div will be hidden after form submission -->
<form id="myForm">

    Name: <input name="name" type="text" /><br/>
    Co-Op Amount: <input name="amount" type="text" /><br/>
    Co-Op Split:<br />
    <input type="radio" name="split" value="100%">100&#37;<br>
    <input type="radio" name="split" value="50/50">50/50<br>
    <input type="radio" name="split" value="75/25">75/25<br>
    Other: <input type="text" name="split" /><br />
    Reason for Co-Op: <input name="reason" type="text" cols="20" rows="5" /><br />
    Brand:  
    <select name="brand">
    <option>Select Option</option>
    <option>Bluebird</option>
    <option>Brown</option>
    <option>Ferris</option>
    <option>Giant Vac</option>
    <option>Honda</option>
    <option>Hurricane</option>
    <option>Little Wonder</option>
    <option>RedMax</option>
    <option>SCAG</option>
    <option>Snapper Pro</option>
    <option>Sno-Way</option>
    <option>SnowEx</option>
    <option>Wright</option>
    <option>Ybravo</option>
    </select><br/>
    Co-Op Category:<br />
    <input type="radio" name="category" id="dealer" value="Dealer Advertising">Dealer Advertising<br />
    <input type="radio" name="category" id="online" value="Digital/Online Marketing">Digital/Online Advertising<br />
    <input type="radio" name="category" id="meetings" value="Meetings and Schools">Meetings and Schools<br />
    <input type="radio" name="category" id="advertising" value="PACE Advertising">PACE Advertising<br />
    <input type="radio" name="category" id="pricing" value="Program Pricing Promotions">Program Pricing Promotions<br />
    <input type="radio" name="category" id="correspondence" value="PACE-to-Dealer Correspondence">PACE-to-Dealer Correspondence<br />
     Other: <input type="text" id="other" name="category" /><br />

    <div class="dealer box" style="display: none;">DEALER</div> 
    <div class="online box" style="display: none;">ONLINE</div> 
    <div class="meetings box" style="display: none;">MEETINGS</div> 
    <div class="advertising box" style="display: none;">ADVERTISING</div> 
    <div class="pricing box" style="display: none;">PRICING</div>  
    <div class="correspondence box" style="display: none;">CORRESPONDENCE</div> 


    Email: <input name="email" type="text" /><br/>
    Message: <textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
    School Schedule (Image Files Only): <input name="myFile" type="file" /><br/>
  <input type="button" value="Submit"
      onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
</form>
</div>

<div id="inProgress" style="display: none;">
<!-- Progress starts hidden, but will be shown after form submission. -->
Uploading. Please wait...
</div>

<div id="output">
  <!-- Blank div will be filled with "Thanks.html" after form submission. -->
</div>

Code.gs:

var submissionSSKey = '1zzRQwgXb0EN-gkCtpMHvMTGyhqrx1idXFXmvhj4MLsk';
var folderId = "0B2bXWWj3Z_tzTnNOSFRuVFk2bnc";

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Form.html');
  template.action = ScriptApp.getService().getUrl();
  return template.evaluate();
}


function processForm(theForm) {
  var fileBlob = theForm.myFile;
  var folder = DriveApp.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);

  // Fill in response template
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  var name = template.name = theForm.name;
  var amount = template.amount = theForm.amount;
  var split = template.split = theForm.split;
  var reason = template.reason = theForm.split;
  var brand = template.brand = theForm.brand;
  var category = template.category = theForm.category;
  var message = template.message = theForm.message;
  var email = template.email = theForm.email; 
  var fileUrl = template.fileUrl = doc.getUrl();

  // Record submission in spreadsheet
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 9).setValues([[name,amount,split,reason,category,brand,message,email,fileUrl]]);

  // Return HTML text for display in page.
  return template.evaluate().getContent();
}

//Toggle Secondary Categories
function(){
    $('input[type="radio"]').click(function(){
        if($(this).attr("id")=="dealer"){
            $(".box").not(".dealer").hide();
            $(".dealer").show();
        }
        if($(this).attr("id")=="online"){
            $(".box").not(".online").hide();
            $(".online").show();
        }
        if($(this).attr("id")=="advertising"){
            $(".box").not(".advertising").hide();
            $(".advertising").show();
        }
        if($(this).attr("id")=="pricing"){
            $(".box").not(".pricing").hide();
            $(".pricing").show();
        }
        if($(this).attr("id")=="correspondence"){
            $(".box").not(".correspondence").hide();
            $(".correspondence").show();
        }
        if($(this).attr("id")=="meetings"){
            $(".box").not(".meetings").hide();
            $(".meetings").show();
        }
        if($(this).attr("id")=="other"){
            $(".box").not(".other").hide();
            $(".other").show();
        }
    });
};

这是我遇到麻烦的地方:

This bit specifically is where I'm having trouble:

//Toggle Secondary Categories
    function(){
        $('input[type="radio"]').click(function(){
            if($(this).attr("id")=="dealer"){
                $(".box").not(".dealer").hide();
                $(".dealer").show();
            }
            if($(this).attr("id")=="online"){
                $(".box").not(".online").hide();
                $(".online").show();
            }
            if($(this).attr("id")=="advertising"){
                $(".box").not(".advertising").hide();
                $(".advertising").show();
            }
            if($(this).attr("id")=="pricing"){
                $(".box").not(".pricing").hide();
                $(".pricing").show();
            }
            if($(this).attr("id")=="correspondence"){
                $(".box").not(".correspondence").hide();
                $(".correspondence").show();
            }
            if($(this).attr("id")=="meetings"){
                $(".box").not(".meetings").hide();
                $(".meetings").show();
            }
            if($(this).attr("id")=="other"){
                $(".box").not(".other").hide();
                $(".other").show();
            }
        });
    };

推荐答案

意外的令牌归因于 function(){行,这对于

The unexpected token is due to the function(){ line, which is invalid syntax for the jQuery document ready function. You should have:

$(function(){
    $('input[type="radio"]').click(function(){
    ...
    });
});

在修复了该错误之后,下一个错误将是:

With that fixed, your next error will be:

未捕获的ReferenceError:未定义$

Uncaught ReferenceError: $ is not defined

那是因为您没有包括jQuery,这是 $ 符号在诸如 $(this)之类的语句中所指的内容.您需要阅读,以获得有关的更多提示在Google Apps脚本中使用jQuery.简而言之:您需要添加以下内容,并根据您打算使用的jQuery版本进行调整:

That's because you haven't included jQuery, which is what the $ symbol is referring to in statements like $(this). You'll want to read this for more tips about using jQuery in Google Apps Script. The short story, though: You need to add the following, adjusted for whatever version of jQuery you intend to use:

<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

更新了Form.html,其中显示了所需的相应< div> .它还包括建议的doctype,html,head和body标签:

Updated Form.html, which shows the appropriate <div> as you intended. It also includes the recommended doctype, html, head and body tags:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
</head>

<body>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
  <script>
    // Javascript function called by "submit" button handler,
    // to show results.
    function updateOutput(resultHtml) {
      toggle_visibility('inProgress');
      var outputDiv = document.getElementById('output');
      outputDiv.innerHTML = resultHtml;
    }

    // From blog.movalog.com/a/javascript-toggle-visibility/
    function toggle_visibility(id) {
      var e = document.getElementById(id);
      if (e.style.display == 'block')
        e.style.display = 'none';
      else
        e.style.display = 'block';
    }

    //Toggle Secondary Categories
    $(function() {
      $('input[type="radio"]').click(function() {
        if ($(this).attr("id") == "dealer") {
          $(".box").not(".dealer").hide();
          $(".dealer").show();
        }
        if ($(this).attr("id") == "online") {
          $(".box").not(".online").hide();
          $(".online").show();
        }
        if ($(this).attr("id") == "advertising") {
          $(".box").not(".advertising").hide();
          $(".advertising").show();
        }
        if ($(this).attr("id") == "pricing") {
          $(".box").not(".pricing").hide();
          $(".pricing").show();
        }
        if ($(this).attr("id") == "correspondence") {
          $(".box").not(".correspondence").hide();
          $(".correspondence").show();
        }
        if ($(this).attr("id") == "meetings") {
          $(".box").not(".meetings").hide();
          $(".meetings").show();
        }
        if ($(this).attr("id") == "other") {
          $(".box").not(".other").hide();
          $(".other").show();
        }
      });
    });
  </script>

  <div id="formDiv">
    <!-- Form div will be hidden after form submission -->
    <form id="myForm">

      Name:
      <input name="name" type="text" /><br/>
      Co-Op Amount: <input name="amount" type="text" /><br/>
      Co-Op Split:<br />
      <input type="radio" name="split" value="100%">100&#37;<br>
      <input type="radio" name="split" value="50/50">50/50<br>
      <input type="radio" name="split" value="75/25">75/25<br> Other: <input type="text" name="split" /><br /> Reason for Co-Op: <input name="reason" type="text" cols="20" rows="5" /><br />
      Brand:
      <select name="brand">
        <option>Select Option</option>
        <option>Bluebird</option>
        <option>Brown</option>
        <option>Ferris</option>
        <option>Giant Vac</option>
        <option>Honda</option>
        <option>Hurricane</option>
        <option>Little Wonder</option>
        <option>RedMax</option>
        <option>SCAG</option>
        <option>Snapper Pro</option>
        <option>Sno-Way</option>
        <option>SnowEx</option>
        <option>Wright</option>
        <option>Ybravo</option>
      </select><br/>
      Co-Op Category:<br />
      <input type="radio" name="category" id="dealer" value="Dealer Advertising">Dealer Advertising<br />
      <input type="radio" name="category" id="online" value="Digital/Online Marketing">Digital/Online Advertising<br />
      <input type="radio" name="category" id="meetings" value="Meetings and Schools">Meetings and Schools<br />
      <input type="radio" name="category" id="advertising" value="PACE Advertising">PACE Advertising<br />
      <input type="radio" name="category" id="pricing" value="Program Pricing Promotions">Program Pricing Promotions<br />
      <input type="radio" name="category" id="correspondence" value="PACE-to-Dealer Correspondence">PACE-to-Dealer Correspondence<br />
      Other: <input type="text" id="other" name="category" /><br />

      <div class="dealer box" style="display: none;">DEALER</div>
      <div class="online box" style="display: none;">ONLINE</div>
      <div class="meetings box" style="display: none;">MEETINGS</div>
      <div class="advertising box" style="display: none;">ADVERTISING</div>
      <div class="pricing box" style="display: none;">PRICING</div>
      <div class="correspondence box" style="display: none;">CORRESPONDENCE</div>


      Email: <input name="email" type="text" /><br/>
      Message: <textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
      School Schedule (Image Files Only): <input name="myFile" type="file" /><br/>
      <input type="button" value="Submit" onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
    </form>
  </div>

  <div id="inProgress" style="display: none;">
    <!-- Progress starts hidden, but will be shown after form submission. -->
    Uploading. Please wait...
  </div>

  <div id="output">
    <!-- Blank div will be filled with "Thanks.html" after form submission. -->
  </div>

</body>

</html>

这篇关于根据单选按钮的选择切换子类别div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆