为什么只有某些javascript函数才能在HTML脚本标签中使用?

斯穆玛

我想我可能对某些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">&nbsp;</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有效-我是否格式化其他函数错误?它们应该在单独的脚本标签中吗?提前致谢!

CanUver

您没有运行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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么只有具有返回值的静态成员函数才能在全局范围内调用?

为什么我不能在Electron中使用脚本标签引用jQuery?

为什么局部变量只有当它是父函数的输入参数时才能在回调函数中访问?

为什么只有主线程才能在Python中设置信号处理程序

为什么只有首页路由才能在实时服务器上运行?

为什么我不能在Flutter中使用某些图标?

为什么我不能在 django 中使用模板标签

为什么只有带有内联类型的函数才能检测到额外的字段?

为什么只有在 for 循环内部才能使用 JavaScript 中的 getElementsByClassName 方法在 <ul> 下成功找到 <li> 或 <span> 标记?

为什么只有自动实现的属性才能在C#中具有初始化程序?

为什么某些HTML5标签必须具有开始标签和结束标签,而不是使用/>自闭标签?

为什么不能在数组内部推送并在javascript的函数参数中使用它?

为什么在 HTML 中使用 Embed 标签?

为什么只有异步函数才能产生异步代码?

为什么不能在函数中使用范围?

为什么我不能在列表中使用匿名函数?

为什么不能在递归函数中使用yield

为什么不能在成员函数中使用这种模式?

为什么不能在函数中使用这些变量?

为什么我不能在Matlab中使用函数readframe

为什么需要main方法才能在类中使用arraylist方法?

为什么只有通过视图菜单工具栏才能使用Firefox的插件?

只有在我按F5时,脚本才能在浏览器中工作

为什么我不能在Google搜索页面等某些网站中使用自定义字体

为什么某些应用程序需要代理才能在某些网络上运行?

为什么只有根记录器才能在具有JSON配置的Log4j 2中运行?

为什么在HTML Javascript中使用<!-和//->?

我需要导入什么才能在UnitTests中使用launchActivity <>()?

为什么只有添加“Thread.sleep(2000);”才能在 Firebase 实时数据库中添加数据