Home  >  Article  >  Backend Development  >  jquery问题求解决

jquery问题求解决

WBOY
WBOYOriginal
2016-06-23 13:49:40844browse

demo3.js

demo3.css

demo3.html

在点击提交的时候直接导航到别的页面去了,加了event.preventDefault()业不起作用


回复讨论(解决方案)

$(document).ready(function(){
$('.error').hide();
$('.submit').click(function(event){
var count=$('input:checked').length;
var amt=0;
if(count==0){
$('p .result').hide();
$('.error').show();
}else{
$('form').find(':checkbox').each(funtion(){
if($(this).is(':checked')){
amt=amt+parseInt($(this).val());
}
});
$('.error').hide();
$('p .result').show();
$('p .result').text('you bill is $'+amt);
}
event.preventDefault();
});
});






检查复选框是否被选中

<script></script>
<script></script>



pizza $5

hotdog $2

coke $3

french fries $6

至少选择一个复选框










.infobox{margin-top:15px;}
.submit{width:100px; height:30px; margin-left:150px; margin-top:10px;}
.error{color:red;}

$(document).ready(function(){	$('.error').hide();$('.result').hide();	$('.submit').click(function(event){		var count=$('input:checked').length;		var amt=0;		if(count==0){			$('p .result').hide();			$('.error').show();		}else{			$('form').find(':checkbox').each(function(){			if($(this).is(':checked')){				amt=amt+parseInt($(this).val());			}			});			$('.error').hide();			$('.result').show();			$('.result').html('you bill is $'+amt);		}	event.preventDefault();return false;	});});

return false 一下即可 就像楼上那样

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn