首页 > 代码库 > Javascript实现checkbox的全选

Javascript实现checkbox的全选

<input type="checkbox" name="all" onclick="checkAll(this)"/>全选<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="checkbox" name="item" value="http://www.mamicode.com/3000" />3000<br />
<input type="button" value="http://www.mamicode.com/总和" onclick="getSum(this)"/> <span id="spanId"></span>

 

<script type="text/javascript">
function getSum()
{
var itemNodes = document.getElementsByName("item");
var sum = 0;
for(var i=0; i<itemNodes.length; i++)
{
if(itemNodes[i].checked)
{
sum += parseInt(itemNodes[i].value);
}
}

var spanNode = document.getElementById("spanId");
var str = sum + "元";
spanNode.innerHTML = str.fontsize(10);
}

function checkAll(checkallNode)
{
var itemNodes = document.getElementsByName("item");
for(var i=0; i<itemNodes.length; i++)
{
itemNodes[i].checked = checkallNode.checked;
}
}

</script>

 

Javascript实现checkbox的全选