如何用JS遍历同类名的文本框

JavaScript017

如何用JS遍历同类名的文本框,第1张

给你个简单的例子吧 以下代码存成html文件,运行即可看出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>无标题文档</title>

<script>

function getlist()

{

var obJh=document.getElementsByName("aa")

for(var i=0i<obJh.lengthi++)

{

alert(obJh[i].value)

}

}

</script>

</head>

<body>

<table width="1000" border="0" cellspacing="0" cellpadding="0">

<tr>

<td>a</td>

<td><input name="aa" type="text" value="a1" /></td>

</tr>

<tr>

<td>b</td>

<td><input name="aa" type="text" value="a2" /></td>

</tr>

</table>

<input name="test" type="button" value="遍历获取" onclick="getlist()" />

</body>

</html>

很简单

$(".class").each(function (){ // 里面放class名字 把你想遍历的标签 class名改成一样的

$(this).val() //值

$(this).attr('id')// id属性

})