日期:2014-05-16  浏览次数:20710 次

Jquery实时计算
想搞个程序,在页面上选择某几项后自动在当前页面计算出价格。
有下面一段程序。它是提交才显示数值的,我是想直接自动在页面下面自动显示,我还想有一个下接选择的。
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
 </HEAD>
 <BODY>
 <form name="form1" id="form1" action ="" method="post" >
 <h1> First type questions</h1>
<p > Question1 :
  <input id="Question1_1_1" name="Question1_1" type="radio" value="0"/>aa
  <input id="Question1_1_2" name="Question1_1" type="radio" value="1"/>bb
  <input id="Question1_1_3" name="Question1_1" type="radio" value="2"/>cc
<p > Question2 :
 <input id="Question1_2_1" name="Question1_2" type="radio" value="0"/>aa
 <input id="Question1_2_2" name="Question1_2" type="radio" value="1"/>bb
 <input id="Question1_2_3" name="Question1_2" type="radio" value="2"/>cc
<p > Question3 :
 <input id="Question1_3_1" name="Question1_3" type="radio" value="0"/>aa
 <input id="Question1_3_2" name="Question1_3" type="radio" value="1"/>bb
 <input id="Question1_3_3" name="Question1_3" type="radio" value="2"/>cc
<p>
<input value="submit" type="submit" name="submit" />
</form>
<script language="javascript">
$(function(){
$('#form1').submit(function(){
var i=0;
$(':radio[name!=Question1_]:checked').each(function(){
i+=$(this).val()*1;
})
alert(i);return false;
});
});
</script>
 </BODY>
</HTML>

------解决方案--------------------
举个例子,供参考:
HTML code
<!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/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
function calc() {
    var q1 = parseInt($("input[name='Question1_1']:checked").val());
    var q2 = parseInt($("input[name='Question1_2']:checked").val());
    var q3 = parseInt($("input[name='Question1_3']:checked").val());
    var q4 = parseInt($("#Question2").val());
        
    $("#demo").html('当前选中结果:' + (q1 + q2 + q3 + q4));
}
$(document).ready( function() {
    $(":radio").bind('change', calc);
    $("select").bind('change', calc);
});
</script>
</head>

<body>
<form name="form1" id="form1" action ="" method="post">
  <h1> First type questions</h1>
  <p> Question1 :
    <input id="Question1_1_1" name="Question1_1" type="radio" value="0" checked="checked" />aa
    <input id="Question1_1_2" name="Question1_1" type="radio" value="1" />bb