getElementsByName Method [Res] When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned. Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME?A HREF="../properties/expando.html">expando.
alert('a');
if(document.getElementsByName("_id0:rad1")[0].checked == true ){
alert('b');
}
}
</script><form id="_id0" method="post" action="/COMS/pages/test.faces" enctype="application/x-www-form-urlencoded">
<table style="border-collapse: collapse">
<tr>
<td>
<label><input type="radio" name="_id0:rad1" onclick="displayTr();"> 是</label>
</td>
<td>
<label><input type="radio" name="_id0:rad1" onclick="displayTr();"> 否</label>
</td>
</tr>
</table>
</form>
现在可以了 测试过了的
虽然table的id="_id0:rad1",但应该用document.getElementsById('_id0:rad1'),才能获得,应该相互不影响才对哈?
有个疑问:可不可以先通过document获得这个table,在获得table包含的radio,这样不晓得行不行,js怎么才能逐级获得语句怎么写?
===================================================================================================================
发帖时掉了,谢谢~~===================================================================================================================
我这个是应为使用jsf标签自动生成的一段点吗,这个标签的命名规则是很奇怪的,他生成的单选项代码自动生成一个包含了table,并且table的id和radio的name相同,为了保持动态代码,所以保留他的这种奇怪的table和radio的命名方式。
代码:<script type="text/javascript">
function displayTr(){
alert('a');
if(document.getElementsByName('_id0:rad1')[0].checked == true ){
alert('b');
}
}
</script><form id="_id0" method="post" action="/COMS/pages/test.faces" enctype="application/x-www-form-urlencoded">
<table id="_id0:rad1" style="border-collapse: collapse">
<tr>
<td>
<label><input type="radio" name="_id0:rad1" value="0" onclick="displayTr();"> 是</input></label>
</td>
<td>
<label><input type="radio" name="_id0:rad1" value="1" onclick="displayTr();"> 否</input></label>
</td>
</tr>
</table>
</form>
When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.
Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME?A HREF="../properties/expando.html">expando.
<script type="text/javascript">
function displayTr(){
alert('a');
if(document.getElementsByName('_id0:rad1')[0].checked == “checked ”){
alert('b');
}
</script>
所以document.getElementsByName('_id0:rad1')[0]指table对象,
document.getElementsByName('_id0:rad1')[1]指第一个radio
document.getElementsByName('_id0:rad1')[2]指第二个radiofunction displayTr(){
var arrRadios = document.getElementsByName('_id0:rad1');
for (var i=0;i<arrRadios.length;i++) {
if (arrRadios[i].type=='radio' && arrRadios[i].checked) {
alert(arrRadions[i].value);
}
}
你用一下:alert(document.getElementsByName('_id0:rad1')[0].innerHTML);
就知道了,可以使用getElementsByTagName或者使用9楼的办法
<script type="text/javascript">
function displayTr(){
alert('a');
if(document.getElementsByName('_id0:rad1')[1].checked == true ){
alert('b');
}
}
</script>
ie 下getElementsByName会把 id,name 属性一样的都取到
ff下就不会。lz的代码在ff(3.09)下是正常的。