简体   繁体   English

如何修复“Uncaught ReferenceError: nameCheck is not defined”错误?

[英]How to fix “Uncaught ReferenceError: nameCheck is not defined” error?

I am having difficulty getting this to work, everything works except the dependent dropdown, am I doing something wrong?我很难让它工作,除了依赖下拉菜单外,一切正常,我做错了什么吗?

I am trying to add this to a google sheet script working with a sheet, it all seems to work except the dependent dropdown.我正在尝试将其添加到使用工作表的谷歌工作表脚本中,除了依赖下拉列表之外,这一切似乎都有效。

This is the error I'm getting:这是我得到的错误:

Uncaught ReferenceError: nameCheck is not defined未捕获的 ReferenceError:未定义 nameCheck

if I test this code on an HTML tester it seems to work correctly, however, it does not seem to work in google sheets.如果我在 HTML 测试仪上测试此代码,它似乎可以正常工作,但是,它似乎不适用于谷歌表格。

<html lang="en">
<body>
<h4>Add a Customer</h4>
 Select a Date: <input type="date" id="date" name="start" class="mb-3">
<div>
     Sales Rep:&nbsp;&nbsp;
    <select id="rep">
        <option>select option</option>
        <option>Neil</option>
        <option>Dave</option>
        <option>Bob</option>
        <option>Trick</option>
    </select>
</div>
 &nbsp;&nbsp;
<div class="add-customer-form">
    <div class="form-group">
        <label for="first-name">Company name : </label>
        <input type="text" class="form-control" id="first-name">
    </div>
    <div>
         Product type:&nbsp;&nbsp;
        <select id="input" onchange="nameCheck()">
            <option>select option</option>
            <option>CASH</option>
            <option>Training</option>
            <option>Optional Modules</option>
            <option>Annual Additional Modules</option>
            <option>Hosting Existing user moving Licence</option>
            <option>Rentals</option>
            <option>Existing Customer - Rentals</option>
            <option>Existing Customer - CASH</option>
            <option>other</option>
            google.script.run.nameCheck();
            console.log(input);
        </select>
        <div>
             Product:&nbsp;&nbsp;
            <select id="output" onchange="nameCheck()">
            </select>
                 

        </div>
    </div>
     &nbsp;&nbsp;
</div>
<div class="form-group">
    <label for="first-name">Quantity : </label>
    <input type="number" class="form-control" id="last-name">
</div>
<div class="form-group mt-3">
    <label for="phone-number">Previous CAD : </label>
    <input type="text" class="form-control" id="phone-number">
    <div class="form-group">
        <label for="sales-price">Sales Price : </label>
        <input type="number" class="form-control" id="salesP">
    </div>
    <div class="form-group">
        <label for="deposit">Deposit : </label>
        <input type="number" class="form-control" id="deposit">
    </div>
    <div class="form-group">
        <label for="install">No. Intallments : </label>
        <input type="number" class="form-control" id="install">
    </div>
    <div class="form-group">
        <label for="invoice">Invoice Nr : </label>
        <input type="text" class="form-control" id="invoice">
    </div>
    <div class="form-group">
        <label for="Notes">Notes : </label>
        <input type="text" class="form-control" id="notes">
    </div>
     &nbsp;&nbsp; <button class="btn btn-primary" id="add-customer-button">Add Customer</button>
</div>
<div class="alert alert-success invisible mt-3" id="save-success-message" role="alert">
     Successfully Added!!
</div>
<script>     

function nameCheck(){
     var a=document.getElementById("input").value;
                console.log(a);
                if(a==="CASH")
                {
                    var arr=["Cash 1","cash2"];
                }
                else if(a==="Existing Customer - CASH")
                {
                    var arr=["existing 1", "existing 2"];
                }
                    else if(a==="Training")
                {
                    var arr=["Level 1 Training (in-house)","Level 2 Training (in-house)","Level 3 Training (in-house)","Onsite Training (up to 4 people)","Training Online per hour","Principles of Design (Renee Mascari)","Graham Hayden Sales/Care","KBBConnect training (per hours)","Training Solus (in-house) - 8 people"];
                }
                var string="";
                for(i=0;i<arr.length;i++)
                {
                    string=string+"<option value="+arr[i]+">"+arr[i]+"</option>";
                }
                document.getElementById("output").innerHTML=string;
            }
                        
        </script>
</body>
</html>

Screenshot off dialog:屏幕截图关闭对话框:

屏幕截图关闭对话框

The code in the question have several issues, ie:问题中的代码有几个问题,即:

  1. Bad HTML坏 HTML

Remove google.script.run.nameCheck();删除google.script.run.nameCheck(); and console.log(input);console.log(input); from

<select id="input" onchange="nameCheck()">
    <option>select option</option>
    <option>CASH</option>
    <option>Training</option>
    <option>Optional Modules</option>
    <option>Annual Additional Modules</option>
    <option>Hosting Existing user moving Licence</option>
    <option>Rentals</option>
    <option>Existing Customer - Rentals</option>
    <option>Existing Customer - CASH</option>
    <option>other</option>
    google.script.run.nameCheck();
    console.log(input);
 </select>

The above because JavaScript can't be included that way between HTML tags.以上是因为 JavaScript 不能以这种方式包含在 HTML 标签之间。

  1. Use of HTML attributes for handling events.使用 HTML 属性来处理事件。

Instead of代替

<select id="input" onchange="nameCheck()">

Use利用

<select id="input">

then between <script></script> use something like然后在<script></script>之间使用类似的东西

document.getElementById('input').onchange = nameCheck;

or use addEventListener或使用addEventListener

Related有关的

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

相关问题 如何修复“ Uncaught ReferenceError:未定义firebase”错误? - How to fix “Uncaught ReferenceError: firebase is not defined” error? 我如何修复错误 Uncaught ReferenceError: addEvent is not defined - How can i fix the error Uncaught ReferenceError: addEvent is not defined 如何修复错误 Uncaught ReferenceError: require is not defined from readline function - how to fix error Uncaught ReferenceError: require is not defined from readline function 如何修复未捕获的 ReferenceError:未定义 TimelineMax - How to fix Uncaught ReferenceError: TimelineMax is not defined 如何修复未捕获的 ReferenceError:熔岩未定义 - How to fix Uncaught ReferenceError: lava is not defined 如何修复未捕获的 ReferenceError:未定义要求 - How to fix Uncaught ReferenceError: require is not defined 未捕获的ReferenceError:错误未定义 - Uncaught ReferenceError: Error not defined 未被捕获的ReferenceError:未定义$错误 - Uncaught ReferenceError: $ is not defined Error 如何修复 Uncaught ReferenceError: require is not defined - 如何修复? - How to fix Uncaught ReferenceError: require is not defined - How to fix it? 如何修复我的代码:错误:未捕获的 ReferenceError:未定义帐户? - How do I fix my code :error: Uncaught ReferenceError: account is not defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM