简体   繁体   English

Javascript 脚本适用于 HTML 页面,但不适用于单独的.js 文件

[英]Javascript script works on the HTML page but not on a separate.js file

I'm building a simple Registration page and am validating some inputs with a .js external file.我正在构建一个简单的注册页面,并使用 .js 外部文件验证一些输入。 The thing is that most of the code works but a small part of it doesn't.问题是大部分代码有效,但一小部分无效。 As you can see I change the status of some fields using document.getElementById(id).disabled = false , for instance.如您所见,例如,我使用document.getElementById(id).disabled = false更改了某些字段的状态。 The code is the same for ids MP1, MP2, and MP3. ids MP1、MP2 和 MP3 的代码相同。 When the procedure calls the script it seems that the MP3 id is not recognized.当程序调用脚本时,似乎无法识别 MP3 id。 The weird part is that if I copy the script onto the main page, alongside the HTML form, and stop using the external file everything works.奇怪的是,如果我将脚本复制到主页上,与 HTML 表单一起,并停止使用外部文件,一切正常。 Does anyone have a clue on how to fix that?有没有人知道如何解决这个问题? I'd like to use an external file instead of writing the script on the main page.我想使用外部文件而不是在主页上编写脚本。

The script below enables all the required fields as expected except for the MP3 id field.下面的脚本按预期启用了除 MP3 id 字段之外的所有必填字段。 It only works when the code is on the body of the main file.它仅在代码位于主文件的主体上时才有效。

 function validarForm() { var optionCheckBox1 = document.getElementById("check-blenda"); if (optionCheckBox1.checked) { document.getElementById("MP3").disabled = false; document.getElementById("perc-mp1").disabled = false; document.getElementById("MP2").disabled = false; document.getElementById("perc-mp2").disabled = false; } else { document.getElementById("perc-mp1").disabled = true; document.getElementById("perc-mp1").value = ""; document.getElementById("MP2").disabled = true; document.getElementById("perc-mp2").disabled = true; document.getElementById("perc-mp2").value = ""; document.getElementById("MP2").value = ""; } }
 <h2>Cadastro de Produtos</h2> <form method="post"> <div class="row"> <div class="col-md-1 mt-3 align-self-center d-flex justify-content-start"> <label for="tipo" class="form-label">Tipo:</label> </div> <div class="col-md-2 mt-3"> <select class="form-select" aria-label="tipo" name="tipo" id="tipo"> <option value="">Selecione o tipo</option> <option value="1">Bobina</option> <option value="2">Saco</option> <option value="3">Folha Cortada</option> </select> </div> <div class="col-md-1 mt-3 align-self-center d-flex justify-content-start"> <label for="config" class="form-label">Configuração:</label> </div> <div class="col-md-3 mt-3"> <select class="form-select" aria-label="config" name="config" id="config"> <option value="">Selecione a Configuração</option> <option value="1"></option> <option value="2">Tubular</option> <option value="3">Folha Simples</option> <option value="4">Folha Dupla</option> <option value="5">Ref. 1 Lado</option> <option value="6">Bolha</option> <option value="7">Gofrada</option> </select> </div> <div class="mt-3 col-md-auto align-self-center d-flex justify-content-start"> <label for="MP" class="form-label">Matéria Prima:</label> </div> <div class="col-md-2 mt-3 align-self-center d-flex justify-content-start"> <select class="form-select" aria-label="MP" name="MP" id="MP"> <option value="">Selecione a MP</option> <option value="1">Reciclado Canela</option> <option value="2">Reciclado Preto</option> <option value="3">Reciclado Colorido</option> <option value="4">Reciclado Cristal</option> <option value="5">PEBD Virgem</option> <option value="6">PEAD Virgem</option> </select> </div> <div class="col-md-1 mt-3 form-check"> <input class="form-check-input" type="checkbox" value="" id="check-blenda" onchange="validarForm()"> <label class="form-check-label d-flex justify-content-center" for="check-blenda">Blenda</label> </div> </div> <div class="row"> <div class="mt-3 col-md-1 align-self-center"> <label for="perc-mp1" class="form-label">% MP 1:</label> </div> <div class="mt-3 col-md-1"> <input type="text" class="form-control" aria-label="perc-mp1" name="perc-mp1" id="perc-mp1" disabled> </div> <div class="mt-3 col-md-auto align-self-center d-flex justify-content-start"> <label for="MP2" class="form-label">Matéria Prima 2:</label> </div> <div class="col-md-2 mt-3 align-self-center d-flex justify-content-start"> <select class="form-select" aria-label="MP2" name="MP2" id="MP2" disabled> <option value="">Selecione a MP</option> <option value="1">Reciclado Canela</option> <option value="2">Reciclado Preto</option> <option value="3">Reciclado Colorido</option> <option value="4">Reciclado Cristal</option> <option value="5">PEBD Virgem</option> <option value="6">PEAD Virgem</option> </select> </div> <div class="mt-3 col-md-1 align-self-center d-flex justify-content-end"> <label for="perc-mp2" class="form-label">% MP 2:</label> </div> <div class="mt-3 col-md-1"> <input type="text" class="form-control" aria-label="perc-mp2" name="perc-mp2" id="perc-mp2" disabled> </div> <div class="mt-3 col-md-auto align-self-center d-flex justify-content-start"> <label for="MP3" class="form-label">Matéria Prima 3:</label> </div> <div class="col-md-2 mt-3 align-self-center d-flex justify-content-start"> <select class="form-select" aria-label="MP3" name="MP3" id="MP3" disabled> <option value="">Selecione a MP</option> <option value="1">Reciclado Canela</option> <option value="2">Reciclado Preto</option> <option value="3">Reciclado Colorido</option> <option value="4">Reciclado Cristal</option> <option value="5">PEBD Virgem</option> <option value="6">PEAD Virgem</option> </select> </div> <div class="mt-3 col-md-1 align-self-center d-flex justify-content-end"> <label for="perc-mp3" class="form-label">% MP 3:</label> </div> <div class="mt-3 col-md-1"> <input type="text" class="form-control" aria-label="perc-mp3" name="perc-mp3" id="perc-mp3" disabled> </div> </div> <div class="mt-4 col-md-auto align-self-center d-flex justify-content-start"> <button type="button" class="btn btn-primary" onclick="validarblendas()">Cadastrar</button> </div> </form>

You is declaring a function, but you never run it, also if yow code will work with the Dom you need to wait after the Dom renders to run it您正在声明一个函数,但您从未运行过它,而且如果您的代码可以与 Dom 一起使用,则您需要在 Dom 渲染后才能运行它

document.addEventListener("DOMContentLoaded", ()=> {



  var optionCheckBox1 = document.getElementById("check-blenda");


  if (optionCheckBox1.checked) {
    document.getElementById("MP3").disabled = false;
    document.getElementById("perc-mp1").disabled = false;
    document.getElementById("MP2").disabled = false;
    document.getElementById("perc-mp2").disabled = false;


  } else {
    document.getElementById("perc-mp1").disabled = true;
    document.getElementById("perc-mp1").value = "";
    document.getElementById("MP2").disabled = true;
    document.getElementById("perc-mp2").disabled = true;
    document.getElementById("perc-mp2").value = "";
    document.getElementById("MP2").value = "";


  }

})

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

相关问题 javascript Intellisense 适用于<script> of html but not in .js file - javascript Intellisense works on <script> of html but not in .js file html页面中的javascript脚本块可以在标头中使用,但不能从外部文件中使用 - javascript script block in html page works in header but not from external file 脚本在html中有效,但在javascript文件中无效 - Script works in html but not in javascript file moment()函数可在html中工作,但不能在单独的javascript文件中工作 - moment() function works in html but not in separate javascript file 为每个html页面使用单独的javascript文件? - Using separate javascript file for each html page? javascript仅适用于<script> tags, not js file - javascript only works in <script> tags, not js file Javascript函数可在HTML页面内运行,但不适用于外部.js文件 - Javascript Function works inside HTML page but doesn't work from external .js file <script> tag works in HTML file but not external .js file - <script> tag works in HTML file but not external .js file 在选项卡中显示带有单独的javascript文件的本地html页面 - Show local html page with separate javascript file in a tab 为什么我的 script.js 文件不起作用,但 index.html 文件中的 JavaScript 代码起作用? - Why my script.js file doesn't work but the JavaScript code inside of the index.html file works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM