<input name="a" value="This is a textbox with name "a"" style="width: 200px;" onclick="checkControlIndex(this);" /> <br /> <input type="button" name="a" value="This is a button with name "a"" onclick="checkControlIndex(this);" /> <br /> <input type="button" name="a" value="This is another button with name "a"" onclick="checkControlIndex(this);" /> <br /> <input type="radio" id="r1" name="a" onclick="checkControlIndex(this);" /> <label for="r1"> This is a radio button with name "a"</label> <br /> <input type="checkbox" id="c1" name="a" onclick="checkControlIndex(this);" /> <label for="c1"> This is a radio button with name "a"</label>
<script type="text/javascript">
function checkControlIndex(control){
var allNameAControls = document.getElementsByName("a");
for(var i=0;i<=allNameAControls.length-1;i++){
if(control == allNameAControls[i]){
alert("第" + (i + 1) + "个控件");
break;
}
}
}
</script>
<input name="a" value="This is a textbox with name "a"" style="width: 200px;" onclick="checkControlIndex(this);" />
<br />
<input type="button" name="a" value="This is a button with name "a"" onclick="checkControlIndex(this);" />
<br />
<input type="button" name="a" value="This is another button with name "a""
onclick="checkControlIndex(this);" />
<br />
<input type="radio" id="r1" name="a" onclick="checkControlIndex(this);" />
<label for="r1">
This is a radio button with name "a"</label>
<br />
<input type="checkbox" id="c1" name="a" onclick="checkControlIndex(this);" />
<label for="c1">
This is a radio button with name "a"</label>