如何在javascript中验证相同的表单id?

如何在javascript中验证相同的表单id?,javascript,html,Javascript,Html,我正在尝试验证两个具有相同id的表单。首先,它工作正常。但第二,这并不能证明为什么? 在这里,我给出了示例代码: function cc(id){ if(document.getElementById(id).value.toUpperCase()==(id)){ document.getElementById(id).style.backgroundColor = "green"; }else{ document.getElementById(id

我正在尝试验证两个具有相同id的表单。首先,它工作正常。但第二,这并不能证明为什么? 在这里,我给出了示例代码:

function cc(id){
    if(document.getElementById(id).value.toUpperCase()==(id)){
        document.getElementById(id).style.backgroundColor = "green";
    }else{
       document.getElementById(id).style.backgroundColor = "red";
    }
}

<form id="save"  method="post">
<input type="text" name="A"  id="C" class="textbox" onKeyUp="cc(id,this, 'H')"  maxlength="1"  />
<input type="text" name="A"  id="C" class="textbox" onKeyUp="cc(id,this, 'H')"  maxlength="1"  />
</form>
函数cc(id){
if(document.getElementById(id).value.toUpperCase()==(id)){
document.getElementById(id).style.backgroundColor=“绿色”;
}否则{
document.getElementById(id).style.backgroundColor=“红色”;
}
}

使用
参数对事件目标进行操作:

function cc(element, goodValue){
    if(element.value.toUpperCase() == goodValue){
        element.style.backgroundColor = "green";
    }else{
        element.style.backgroundColor = "red";
    }
}

<form id="save"  method="post">
<input type="text" name="A"  id="C" class="textbox" onKeyUp="cc(this, 'H')"  maxlength="1"  />
<input type="text" name="A"  id="C" class="textbox" onKeyUp="cc(this, 'H')"  maxlength="1"  />
</form>
函数cc(元素,goodValue){
if(element.value.toUpperCase()==goodValue){
element.style.backgroundColor=“绿色”;
}否则{
element.style.backgroundColor=“红色”;
}
}

id应该是唯一的“尝试验证两个具有相同id的表单”-不要。ID必须是唯一的。既然将
作为参数传递,为什么需要使用
getElementById()
。另外,在将其作为参数传递之前,您没有设置
id
。这就是我要做的。但是,这并不是在验证。你可以在link@Quentin中查看