直接上代碼:
function Sum(obj)
{
if(isNaN($(obj).val()))
{
alert("請輸入1-10的數字。");
$(obj).attr("value","");
$(obj).focus();
return false;
}
var total = 0;
var count = 0;
$("input[type='text']").each( function (i) {
count++;
if($( this).attr("t")=="1")
{
if($( this).val()!="")
total+= parseInt($( this).val());
}
});
var average = (total/count).toFixed(2);
$("#<%=labAverageScore1.ClientID%>").html(average);
return false;
}
{
if(isNaN($(obj).val()))
{
alert("請輸入1-10的數字。");
$(obj).attr("value","");
$(obj).focus();
return false;
}
var total = 0;
var count = 0;
$("input[type='text']").each( function (i) {
count++;
if($( this).attr("t")=="1")
{
if($( this).val()!="")
total+= parseInt($( this).val());
}
});
var average = (total/count).toFixed(2);
$("#<%=labAverageScore1.ClientID%>").html(average);
return false;
}
輸入框設置:
<
asp:TextBox
ID
="TextBox1"
runat
="server"
Width
="50px"
t
="1"
onblur
="Sum(this);"
></
asp:TextBox
>
< asp:TextBox ID ="TextBox2" runat ="server" Width ="50px" t ="1" onblur ="Sum(this);" ></ asp:TextBox >
< asp:TextBox ID ="TextBox2" runat ="server" Width ="50px" t ="1" onblur ="Sum(this);" ></ asp:TextBox >
注意輸入框里面有個自定義屬性t="1",這就是一個標識,默認輸入框是沒有這屬性的,只管放上去就行,不要管他,這是一個小技巧。