答案 0 :(得分:1)
您应该使用表单包装输入并绑定onsubmit事件:
function WoodFunct() {
var areaLength = document.getElementById("textbox1").value;
var areaWidth = document.getElementById("textbox2").value;
var areaDepth = document.getElementById("textbox3").value;
var answer =(parseFloat(areaLength)*parseFloat(areaWidth)*parseFloat(areaDepth))*27;
var textbox4 = document.getElementById('textbox4');
textbox4.value=answer;
}
function DirtFunct() {
var areaLength = document.getElementById("textbox1").value;
var areaWidth = document.getElementById("textbox2").value;
var areaDepth = document.getElementById("textbox3").value;
var answer =(parseFloat(areaLength)*parseFloat(areaWidth)*parseFloat(areaDepth))*1.5;
var textbox4 = document.getElementById('textbox4');
textbox4.value=answer;
}
document.getElementById('form').onsubmit = function() {
if (document.getElementById('select').value == 1) {
WoodFunct();
}
else {
DirtFunct();
}
return false;
}

<form id="form">
L: <input type="text" name="textbox1" id="textbox1" /><br>
W: <input type="text" name="textbox2" id="textbox2" /> <br>
D: <input type="text" name="textbox3" id="textbox3" /> <br>
Product:
<select id="select">
<option value='1' id="WoodProduct">Wood</option>
<option value='2' id="DirtProduct">Dirt</option>
</select><br>
<input type="submit" name="button" id="button1" value="Submit" />
<br/>
Needed sq. footage:
<input type="text" name="textbox4" id="textbox4" readonly="true"/>
</form>
&#13;
答案 1 :(得分:0)
如果你使用jquery,你可以做
$(function() {
$("#button1").click(function(){
var selected = $("#select option:selected").text();
if(selected === 'Wood')
WoodFunct();
else if(selected === 'Dirt')
DirtFunct();
});
});
答案 2 :(得分:0)
编写运行函数的简单方法是检查是否选择了每个选项。像这样。
function runAFunction() {
if(document.getElementById("WoodProduct").selected = true)
WoodFunct();
else if(document.getElementById("DirtProduct").selected = true)
DirtFunct();
}