分配var ID而不是名称

时间:2019-03-09 20:43:08

标签: forms id

经过几天的搜索,我不得不问专家-不是。 我在一页上有几种用于各种功能的JS计算器。 还有多种形式(因为我不知道如何组合它们)。 所有形式的一个输入框都使用name =“ Time”。所有的表格都运行良好。

如何将脚本从使用“名称”更改为使用“ id”?

JS是

function calc(form) {
var W,T
W=form.Weight.value*form.WeightUnit.options[form.WeightUnit.selectedIndex].value
T=form.Time.value/form.TimeUnit.options[form.TimeUnit.selectedIndex].value
form.Calories.value = Math.round(form.Activity.options[form.Activity.selectedIndex].value*W*T)
}

...在表格中使用HTML ...

<form name="form_3">
<input name="Weight" id="Weight" size="5" type="text" /><select name="WeightUnit"><option value="1">Pounds</option><option value="2.2">Kilograms</option></select> <select name="Activity"><option value="0">Select an Activity</option><option value="2.65">Aerobic Dance (Casual)</option><option value="3">Aerobic Dance (Moderate)</option><option value="3.71">Aerobic Dance (Intense)</option><option value="2.7">Basketball (Full court)</option><option value="1.67">Basketball (Half court)</option><option value="2">Calisthenics</option><option value="1.6">Cycling (5.5 mph, 9 kmph)</option><option value="3">Cycling (10 mph, 16 kmph)</option><option value="4.26">Cycling (13 mph, 21 kmph)</option><option value="4.66">Cycling (Racing)</option><option value="2.62">Dancing (Casual)</option><option value="2.84">Dancing (Moderate)</option><option value="3.65">Dancing (Extreme)</option><option value="0.6">Driving</option><option value="3.7">Field Sports (Competitive)</option><option value="2.75">Frisbie</option><option value="2.37">Golf (Carry Clubs)</option><option value="2">Golf (with Cart)</option><option value="1.96">Gymnastics</option><option value="1.7">Housework</option><option value="4.6">Jump Rope (Fast)</option><option value="3.7">Jump Rope (Slow)</option><option value="0.6">Lying</option><option value="5.35">Martial Arts</option><option value="4.88">Racquetball</option><option value="3.6">Rollerblading (Casual)</option><option value="4.25">Rollerblading (Fast)</option><option value="4.91">Rowing Machine (Extreme)</option><option value="3.27">Rowing Machine (Moderate)</option><option value="2.92">Running (4 mph, 6.5 kmph)</option><option value="3.62">Running (5 mph, 8 kmph)</option><option value="4.8">Running (6 mph, 9.7 kmph)</option><option value="5.4">Running (7 mph, 11.3 kmph)</option><option value="6.3">Running (8 mph, 13 kmph)</option><option value="7.5">Running (10 mph, 16 kmph)</option><option value="7.9">Running (12 mph, 19.3 kmph)</option><option value="0.76">Sitting (Office work)</option><option value="0.6">Sitting (Quietly)</option><option value="0.3">Sleeping</option><option value="1.91">Softball</option><option value="3.71">Stair Step (6", 15.2 cm)</option><option value="4.09">Stair Step (8", 20.3 cm)</option><option value="4.42">Stair Step (10", 25.4 cm)</option><option value="4.91">Stair Step (12", 30.5 cm)</option><option value="0.98">Standing (Office work)</option><option value="0.76">Standing (Quietly)</option><option value="3">Swimming (Casual)</option><option value="10">Swimming (Vigorous)</option><option value="2.56">Tennis (Doubles)</option><option value="3">Tennis (Singles)</option><option value="1.39">Volleyball (Casual)</option><option value="3.98">Volleyball (Vigorous)</option><option value="1.4">Walking (2 mph, 3.2 kmph)</option><option value="1.75">Walking (2.5 mph, 4 kmph)</option><option value="2">Walking (3 mph, 4.8 kmph)</option><option value="2.3">Walking (3.5 mph, 5.6 kmph)</option><option value="2.67">Walking (4 mph, 6.5 kmph)</option><option value="3.1">Walking (4.5 mph, 7.2 kmph)</option><option value="1.0">Waxing Car</option><option value="3.2">Weight Training (Intense)</option><option value="2.35">Weight Training (Normal)</option><option value="3.4">Yardwork (Heavy)</option><option value="1.6">Yardwork (Regular)</option></select> <input name="Time" size="5" type="text" /><select name="TimeUnit"><option value="60">Minutes</option><option value="1">Hours</option></select> <input onclick="calc(this.form)" type="button" value="Calculate" /><input name="Reset" type="reset" value="Reset" /> <input name="Calories" size="5" type="text" />
</form>

该计算工作正常,但是我“需要”向表单添加更多功能,并且使用“ name”将不起作用,因为在所有计算表单中都使用了name =“ Time”。 因此,我试图找出如何使用id而不是var T的名称。

对我来说,让计算器计算“和”然后分配其他功能是不可能的,因为我一直想找出如何在所有形式中使用id而不是名称。

我敢肯定有很多方法,但是我似乎找不到。所以请帮忙。

我希望您也可以提供更新的HTML的示例。 谢谢-提前。

0 个答案:

没有答案