Javascript 如何禁用多个输入文本字段?
单击“提交”按钮后,如何禁用多个输入文本字段 html代码:Javascript 如何禁用多个输入文本字段?,javascript,html,Javascript,Html,单击“提交”按钮后,如何禁用多个输入文本字段 html代码: <input type="text" placeholder="YOUR NAME" id="name"> //first input text field <input type="text" placeholder="YOUR NAME" id="name"> //second input text field <button onclick="myFunction()">Submit&l
<input type="text" placeholder="YOUR NAME" id="name"> //first input text field
<input type="text" placeholder="YOUR NAME" id="name"> //second input text field
<button onclick="myFunction()">Submit</button>
此代码仅适用于第一个输入文本字段,而不适用于第二个输入文本字段。我想禁用两者。
请帮助PPP
ID
是唯一的。不能有两个id相同的id
元素。改为使用querySelectorAll
const inputs=document.querySelectorAll('input');
函数myFunction(){
inputs.forEach(输入=>{
input.disabled=true;
});
}
提交
使用类选择器而不是id!将id转换为类并使用document.querySelectorAll(“.name”).forEach(输入=>input.disabled=true)@sarasiddiqui-您还需要选择textarea
元素。i:edocument.queryselectoral('input,textarea')代码>请参见编辑。@sarasiddiqui-如果答案解决了您的问题,请将其标记为已接受,以表明问题已解决。我还有一个问题。禁用输入文本字段后,背景颜色变为灰色。如何更改我选择的背景色?@sarasiddiqui-您可以通过设置元素的style
属性,将css
样式添加到元素中。i:einput.style.background
。我已经编辑了第二个示例。
function myFunction() {
document.getElementById("name").disabled = true;
}