I am creating a form using Gridview in ASP.NET. When the user fills in Qty and Price it needs to automatically calculate the Total. However, my Total always returns Nan (Not a number). What am I doing wrong?
The ASP page looks like this:
JavaScript
x
27
27
1
<asp:GridView ID="GridView1" runat="server"
2
AutoGenerateColumns="false">
3
<Columns>
4
<asp:BoundField DataField="Item" HeaderText="Item" />
5
<asp:BoundField DataField="Price" HeaderText="Item_Desc" ItemStyle-CssClass="price" />
6
<asp:TemplateField HeaderText="Jumlahdatang" >
7
<ItemTemplate >
8
<asp:TextBox ID="Qty" runat="server" ></asp:TextBox>
9
</ItemTemplate>
10
</asp:TemplateField>
11
12
<asp:TemplateField HeaderText="Harga Satuan" >
13
<ItemTemplate>
14
<asp:TextBox ID="price" runat="server" ></asp:TextBox>
15
</ItemTemplate>
16
</asp:TemplateField>
17
18
<asp:TemplateField HeaderText="Harga Total" >
19
<ItemTemplate >
20
<asp:Label ID="total" runat="server" Text="0" ></asp:Label>
21
22
</ItemTemplate>
23
24
</asp:TemplateField>
25
</Columns>
26
</asp:GridView>
27
The JavaScript:
JavaScript
1
38
38
1
<script type="text/javascript"
2
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
3
<script type="text/javascript">
4
// give value 0 first time
5
$(function() {
6
$("[id*=Qty]").val("0");
7
$("[id*=price]").val("0");
8
$("[id*=total1]").val("0");
9
});
10
//check data must number on Qty
11
$("[id*=Qty]").live("change", function() {
12
if (isNaN(parseInt($(this).val()))) {
13
$(this).val('0');
14
} else {
15
$(this).val(parseInt($(this).val()).toString());
16
}
17
});
18
//check data must number on price
19
$("[id*=price]").live("change", function() {
20
if (isNaN(parseInt($(this).val()))) {
21
$(this).val('0');
22
} else {
23
$(this).val(parseInt($(this).val()).toString());
24
}
25
});
26
27
$("[id*=price]").live("keyup", function() {
28
if (!jQuery.trim($(this).val()) == '') {
29
if (!isNaN(parseFloat($(this).val()))) {
30
var row = $(this).closest("tr");
31
$("[id*=total]", row).html(parseFloat($(".Qty", row).html()) * parseFloat($(this).val()));
32
}
33
} else {
34
$(this).val('');
35
}
36
37
});
38
Advertisement
Answer
JavaScript
1
2
1
if (isNaN(parseInt($(this).val()))) {
2
why are you using isNan and parseInt at the same time ?
i think is should be :
JavaScript
1
2
1
if (isNaN($(this).val()))
2
which returns true if val = string and false if number
another one.. you only need to parseInt if you will use their value in a formula ..