我想我可能对某些JavaScript函数的格式设置有误-我是html的新手。其中只有一个有效。它们都是script标记,但包含完整的程序。这是代码:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>State Tax Calculator</title>
<body>
<form name="stateSelect">
<!sets value to options, tax rate to state>
Select which state you are purchasing from:
<select id="stateOptions" onchange="showData()">
<option value='.04'>Alabama</option>
<option value='0'>Alaska</option>
<option value='.0560'>Arizona</option>
</select>
</form>
<p id="firstP"> </p>
<script>
function showData() {
//extracts value from option selected
var theSelect = stateSelect.stateOptions;
var firstP = document.getElementById('firstP');
firstP.innerHTML = ('Your state retail tax rate is: ' +
theSelect[theSelect.selectedIndex].value * 100 + '%');
}
function askPrice() {
//asks the user for the price of their item
var price = Window.prompt("What's the price of your retail item?","0");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
}
var num1 = parseInt(price);
}
function finalCost(){
//multiplies the original price with the state tax rate to get final cost
return askPrice * theSelect[theSelect.selectedIndex].value;
}
</script>
</body>
</html>
仅函数showData有效-我是否格式化其他函数错误?它们应该在单独的脚本标签中吗?提前致谢!
您没有运行askPrice和finalCost函数吗?这些功能需要根据特定功能来工作,但是您尚未在页面中定义它们。
我想这就是你想做的。我把所有功能都放在一个功能之下。
现在,当您在选择框上进行更改时,它会从用户那里接收价格信息,并根据屏幕上的价格信息显示更改。
既然您没有确切说明您想要什么,那就是我所能做的。如果您了解自己的逻辑,则可以进行必要的安排。
let firstP = document.getElementById('firstP');
function showData() {
let theSelect = stateSelect.stateOptions;
let selectValue = theSelect[theSelect.selectedIndex].value;
let getUserPrice = window.prompt("What's the price of your retail item?","0");
firstP.innerHTML = 'Your state retail tax rate is:' + (getUserPrice * parseFloat(selectValue)) + "%"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句