0

かなり複雑な配列コードを適応させようとしています。基本的には、入力した合計時間を足して、その値をinput name="total_hrs"に返したいのですが、行き詰まりました!また、スクリプトを簡素化/クリーンアップするための助けをいただければ幸いです! ありがとう。スクリプトは次のとおりです。

<script language="JavaScript" type="text/javascript">
<!--
function subCalc(the_form) 
{
var subtotal = 0;
var subtemp = 0;
var totalHrs = 0;

// put the calories and form fields into parallel arrays.
var calorie_array = new Array()
calorie_array[0] = 72;
calorie_array[1] = 86;
calorie_array[2] = 100;
calorie_array[3] = 126;
calorie_array[4] = 130;
calorie_array[5] = 180;
calorie_array[6] = 156;
calorie_array[7] = 182;
calorie_array[8] = 210;
calorie_array[9] = 250;
calorie_array[10] = 270;
calorie_array[11] = 390;
calorie_array[12] = 390;
calorie_array[13] = 546;

var time_array = new Array()
time_array[0] = the_form.sleeping_time;
time_array[1] = the_form.TV_time;
time_array[2] = the_form.sit_time;
time_array[3] = the_form.cook_time;
time_array[4] = the_form.stand_time;
time_array[5] = the_form.wash_time;
time_array[6] = the_form.walk_S_time;
time_array[7] = the_form.house_time;
time_array[8] = the_form.walk_M_time;
time_array[9] = the_form.gard_time;
time_array[10] = the_form.danc_time;
time_array[11] = the_form.stairs_time;
time_array[12] = the_form.jog_time;
time_array[13] = the_form.sqsh_time;

var sub_array = new Array()
sub_array[0] = the_form.cal0;
sub_array[1] = the_form.cal1;
sub_array[2] = the_form.cal2;
sub_array[3] = the_form.cal3;
sub_array[4] = the_form.cal4;
sub_array[5] = the_form.cal5;
sub_array[6] = the_form.cal6;
sub_array[7] = the_form.cal7;
sub_array[8] = the_form.cal8;
sub_array[9] = the_form.cal9;
sub_array[10] = the_form.cal10;
sub_array[11] = the_form.cal11;
sub_array[12] = the_form.cal12;
sub_array[13] = the_form.cal13;


for(i = 0; i < calorie_array.length; i++) 
{
// Give subtemp the value or the calorie times the time.
subtemp = (calorie_array[i] * time_array[i].value);

// Put the converted string into the form field.
sub_array[i].value = checkAmount(subtemp);

// Add the converted number value to subtotal.
subtotal += roundFloat(subtemp);

}
for(i = 0; i < time_array.length; i++) 
{
hours = (time_array[i].value);
totalHrs += roundFloat(hours);
} 

return subtotal;
}

function totalCalc() {
var form;
var subtotal;
var total;
var totalHrs;
var hours;

form = document.calc_form;
// get the value of subtotal from totalCalc.
subtotal = subCalc(form);
totalHrs = subCalc(form);

// Add the NUMBER values and subtotal.
total = subtotal;

// Convert this number into a string and display.
form.total.value = checkAmount(total);

// Adds total hours.
hours = totalHrs
form.total_hrs.value = checkAmount(hours);
}


function roundFloat(num) {
num = parseFloat(num);
num = Math.round(100*num)/100

return num
}

function checkAmount(num) {
// Convert into a floating point number.
num = parseFloat(num)
// Round the number off.
num = Math.round(100*num)/100
// Turn into a string.
num = String(num)
// Return the converted string.
return num
}

//-->
</script>

そしてhtml:

<form name="calc_form" id="form" method="post">
<table width="310" border="0" bgcolor="#EAEAEA">
<tr>
<th width="95"><h2>Activity</h2></th><th width="70"><h2>Time <br>(in hours)</h2></th>
<th width="101"><h2>Calories used per hour</h2></th>
</tr>
<tr class="table-text">
<td class="right"><p class="table-text">Sleeping</p></td>
<td width="70">
<p class="table-text">
<input name="sleeping_time" type="text" id="sleeping_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101">
<p class="table-text">
<input name="cal0" type="text" id="cal0" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Eating/Reading/<br>Watching TV</p></td>
<td width="70"><p class="table-text">
<input name="TV_time" type="text" id="TV_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal1" type="text" id="cal1" size="6" maxlength="6" />
</span>kcals</td>
</tr>
<tr>
<td class="right"><p class="table-text">Sitting</p></td>
<td width="70"><p class="table-text">
<input name="sit_time" type="text" id="sit_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal2" type="text" id="cal2" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Cooking</p></td>
<td width="70"><p class="table-text">
<input name="cook_time" type="text" id="cook_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal3" type="text" id="cal3" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Standing</p></td>
<td width="70"><p class="table-text">
<input name="stand_time" type="text" id="stand_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal4" type="text" id="cal4" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Washing &amp; Dressing</p></td>
<td width="70"><p class="table-text">
<input name="wash_time" type="text" id="wash_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal5" type="text" id="cal5" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Walking Slowly</p></td>
<td width="70"><p class="table-text">
<input name="walk_S_time" type="text" id="walk_S_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal6" type="text" id="cal6" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Light housework</p></td>
<td width="70"><p class="table-text">
<input name="house_time" type="text" id="house_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal7" type="text" id="cal7" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Walking moderately</p></td>
<td width="70"><p class="table-text">
<input name="walk_M_time" type="text" id="walk_M_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal8" type="text" id="cal8" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Light gardening</p></td>
<td width="70"><p class="table-text">
<input name="gard_time" type="text" id="gard_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal9" type="text" id="cal9" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Dancing</p></td>
<td width="70"><p class="table-text">
<input name="danc_time" type="text" id="danc_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal10" type="text" id="cal10" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Walking up stairs</p></td>
<td width="70"><p class="table-text">
<input name="stairs_time" type="text" id="stairs_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal11" type="text" id="cal11" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Jogging</p></td>
<td width="70"><p class="table-text">
<input name="jog_time" type="text" id="jog_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal12" type="text" id="cal12" size="6" maxlength="6" />
kcals</p></td>
</tr>
<tr>
<td class="right"><p class="table-text">Squash</p></td>
<td width="70"><p class="table-text">
<input name="sqsh_time" type="text" id="sqsh_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="cal13" type="text" id="cal13" size="6" maxlength="6" />
kcals</p></td>
</tr>

<tr>
<td><p class="table-text">&nbsp;</p></td>
<td width="70"><p class="table-text"></p></td>
<td width="101"><p class="table-text"></p></td>
</tr>

<tr>
<td><p class="table-text"><strong>Totals = </strong></p></td>
<td width="70"><p class="table-text">
<input name="total_hrs" type="text" id="total_hrs" size="3" />
hrs</p></td>
<td width="101"><p class="table-text">
<input name="total" type="text" id="total" size="6" />
kcals</p></td>
</tr> 

<tr> 
<td colspan="2"><INPUT name="reset" value="Reset" TYPE="reset"> </td>
<td colspan="2"><input name="Calculate Total" type="button" id="Calculate Total" value="Calculate Total" onclick="totalCalc();" /></td>
</tr>
</table>
</form> 
4

2 に答える 2

1

この行を確認してください

subtotal = subCalc(form);
totalHrs = subCalc(form);

関数 subCalc を見ると、常に小計を返しているため、2 行目が正しくありません。

この機能を 2 つに分割できます。1 つは合計を返し、もう 1 つは時間を返します。

あなたのプロジェクトで頑張ってください!!

于 2013-01-02T20:49:44.533 に答える
0

ここでコードの修正版を作成しました: http://pastebin.com/7L7i7dSk

便宜上、スクリプトと HTML を 1 つのドキュメントにまとめました。他に理由はありません。必要に応じて、それらを分離することもできます。

以下が変更されました。

  • 68 行目。+数値に値を強制する which タイプを追加しました。

  • 72 行目。関数subCalcは次のような配列を返すようになりました。

    return [subtemp, subtotal, totalHrs]

    (実際に subtemp を使用する予定があるかどうかはわかりませんが、含めました。)

  • line 81. という名前の新しい配列totalsが宣言されています

  • 85 行目。新しい配列には、次の戻り値が取り込まれます。 subCalc

  • 行 89-90。subtotaltotalHrs、それぞれの値が与えられます。(これは、私が知る限り、元のコードの問題でした。両方に同じ値、つまり総カロリーが与えられていました)。

于 2013-01-02T20:32:35.830 に答える