我试图测试一个java脚本函数,该函数的目的是获取用户选择的表单元素的值。特别是一个单选按钮。我的目标是在另一个函数中使用此函数来计算总价。但就目前而言,我只想确保此函数返回所选单选按钮的值。
<html>
<head>
</head>
<body>
<form name="testForm">
<legend>Calculate your square feet</legend>
</br>
<label> What size plants are you starting with ? </label>
</br>
</br>
<input type="radio" name="plantType" value="16"/>Seeds, havn't started yet</label>
</br>
<input type="radio" name="plantType" value="16"/> Seedlings, less than 2ft high</label>
</br>
<input type="radio" name="plantType" value="32"/> Juvenile, 2 - 4ft high</label>
</br>
<input type="radio" name="plantType" value="64"/> Adult, 4ft or higher</label>
</br>
</form>
<script type="text/javascript">
var plant_name = new Array();
plant_name[16] = 16;
plant_name[16] = 16;
plant_name[32] = 32;
plant_name[32] = 64;
function getPlantSize() {
var plantSize = 0;
var theForm = document.testForm;
var selectedPlant = theForm.plantType;
for (var i = 0; i < plantType.length; i++)
{
if (plantType[i].checked)
{
plantSize = plant_name.[selectedPlant[i].value];
break;
}
document.write (plantSize);
}
}
</script>
</body>
</html>