<script type="text/javascript">
/*全选*/
function checkAll(){
var checkname=document.getElementsByName("items");
for (var i = 0; i < checkname.length; i++) {
checkname[i].checked = event.srcElement.checked;
}
total11();
}
/*计算选中的价格*/
function total11(){
var sum=0;
var checkname=document.getElementsByName("items");
for (var i = 0; i < checkname.length; i++) {
if(checkname[i].checked){
sum+= parseInt(checkname[i].value);
}
} }
/*选中某一个计算价格*/
function iniEvent() {
var checkname = document.getElementsByName("items");
for (var i = 0; i < checkname.length; i++) {
checkname[i].onclick = total11;
}
}
/*提交表单,并写入到cook,已经使用flag判断是否有一个被选中*/
function submitfrom() {
var checkvalue=‘‘;
var flag = false;
var checkname=document.getElementsByName("items");
for (var i = 0; i < checkname.length; i++) {
if(checkname[i].checked){
checkvalue+= checkname[i].value+‘,‘;
flag = true ;
setcookie(‘items‘,checkvalue,setcookieday(10));
}
}
if(!flag){
alert("请最少选择一项!");
return false ;
}
}
function setcookie(name,value,expires,path,domain,secure){
var cookiename=encodeURIComponent(name)+‘=‘+encodeURIComponent(value);
if(expires instanceof Date){
cookiename+=‘;expires=‘+expires;
}
if(path){
cookiename+=‘;path=‘+path;
}
if(secure){
cookiename+=‘;secure‘;
}
document.cookie=cookiename;
}
function setcookieday(day){
var date=null;
if(typeof day == ‘number‘&& day>0){
date = new Date();
date.setDate(date.getDate()+day);}
return date;
}
</script>
如何调用例子
<body onload="iniEvent()"> | |
<div id="divMain"> | |
<p><input type="checkbox" name="??" onclick="checkAll()" />??</p> | |
<p><input type="checkbox" name="item" value="2999" />??2999 Haier/???? KFR-35GW/03GJC12-DS</p> | |
<p><input type="checkbox" name="item" value="2256" />??2256 ??è??? AUX/???? KFR-35GW/SFA+2</p> | |
<p><input type="checkbox" name="item" value="2799" />??2799 Gree/???? KFR-26G(26556)FNDe-3</p> | |
<p><input type="checkbox" name="item" value="2008" />??2008 AUX/???? KFR-25GW/SFA+2</p> | |
<p><input type="checkbox" name="item" value="2598" />??2598 Hisense/???? KFR-35GW/EF11S3</p> | |
<p><input type="checkbox" name="item" value="1877" />??1877 TCL KFRd-25GW/DE22</p> | |
<p><input type="checkbox" name="item" value="2398" />??2398 Kelon/???? KFR-35GW/EFVCS3</p> | |
<p><input type="checkbox" name="item" value="1999" />??1999 TCL KFRd-35GW/FC23</p> | |
<p><input type="checkbox" name="??" onclick="checkAll()" />??</p> | |
<p><span id="sumMoney">????:</span></p> | |
</div> | |
</body> |