プロが教えるわが家の防犯対策術!

<table>

<tr>
<td class="point"><input type="text"></td>
<td class="point"><input type="text"></td>
<td class="point"><input type="text"></td>
<td class="point"><input type="text"></td>
</tr>

</table>

とHTMLで記述していて、jQueryを下記のように記述しました。
var total;
j$(".point:text").change(function(){
j$(".point:text").each(function(){
var point = j$(this).val();
total = parseInt(point)+total;
});
alert(total);
});

alertで返ってくる結果が「NaN」となります。
どのようにすれば合計値が求められるでしょうか。

やりたいことは「pointクラス」内のテキストフォームの値を合計したいです。

A 回答 (1件)

こんな感じで如何でしょう。



<html>
<head>
<title>テスト</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7 …
<script type='text/javascript'>
$(document).ready(function(){
var total;
$(".point input:text").change(function(){
total=0;
$(".point input:text").each(function(){
var point = $(this).val();
total = parseInt(point)+total;
});
alert(total);
});
});
</script>
</head>
<body>
<table>
<tr>
<td class="point"><input type="text" value="0" /></td>
<td class="point"><input type="text" value="0" /></td>
<td class="point"><input type="text" value="0" /></td>
<td class="point"><input type="text" value="0" /></td>
</tr>
</table>
</body>
</html>
    • good
    • 0
この回答へのお礼

ありがとうございます。
できました!!!

お礼日時:2012/06/07 15:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!