简体   繁体   English

JavaScript函数仅适用于具有类名称的第一个元素,而不适用于具有该类的所有元素

[英]JavaScript function is only working for the first element with a class name, not all elements with that class

I have multiple forms on one page with same structure. 我在一页上有多个具有相同结构的表格。

They all have a file input field that has 'onchange' event, which removes the 'disabled' attribute from my submit button when a file is chosen. 它们都有一个具有“ onchange”事件的文件输入字段,当选择一个文件时,该字段将从我的提交按钮中删除“ disabled”属性。

The problem is, my function only works for the first element with that class name. 问题是,我的函数仅适用于具有该类名称的第一个元素。 How can I make it work for every item with that class? 如何使该类的每个项目都能使用?

index.php: index.php文件:

<form class="form">
    <input type="file" name="image" onchange="unlock();">
    <input type="text" name="title" placeholder="Image title"/>
    <input type="submit" value="Add image" class="submit" disabled/>
</form>

<form class="form">
    <input type="file" name="image" onchange="unlock();">
    <input type="text" name="title" placeholder="Image title"/>
    <input type="submit" value="Add image" class="submit" disabled/>
</form>

scripts.js: scripts.js中:

function unlock() {
    document.querySelector('.submit').removeAttribute("disabled");
}

Any help is appreciated! 任何帮助表示赞赏!

  1. Use eventListener 使用eventListener
  2. address relatively using selectors 使用选择器相对解决
  3. make it a habit to never call anything submit in a form 养成一个习惯,从不叫任何东西以表格形式提交

 document.querySelectorAll("[name=image]").forEach( ele => ele.addEventListener("change", (e) => e.target.closest(".form").querySelector(".sub").disabled = false ) ); 
 <form class="form"> <input type="file" name="image"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" value="Add image" class="sub" disabled/> </form> <form class="form"> <input type="file" name="image"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" value="Add image" class="sub" disabled/> </form> 

More compatible version 兼容版本

 [...document.querySelectorAll("[name=image]")].forEach(function(ele) { ele.addEventListener("change", function(e) { e.target.parentNode.querySelector(".sub").disabled = false; }) }); 
 <form class="form"> <input type="file" name="image"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" value="Add image" class="sub" disabled/> </form> <form class="form"> <input type="file" name="image"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" value="Add image" class="sub" disabled/> </form> 

Give ID's to all submit buttons and pass the respective ID's in unlock() function. 给所有提交按钮提供ID,并在unlock()函数中传递相应的ID。

This removes the disabled from the submit 这会将残疾人从提交中删除

 function unlock(form_id) { document.getElementById(form_id).removeAttribute("disabled"); } 
 <form class="form"> <input type="file" name="image" onchange="unlock('form1');"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" id="form1" value="Add image" class="submit" disabled/> </form> <form class="form"> <input type="file" name="image" onchange="unlock('form2');"> <input type="text" name="title" placeholder="Image title" /> <input type="submit" id="form2" value="Add image" class="submit" disabled/> </form> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM