javascript - 单击时未调用函数?

标签 javascript

简单的 Madblib 项目。或者说我是这么想的。这可能是一个简单的修复,但我非常感谢任何帮助,我似乎无法让它正常工作。

单击按钮时不会调用该函数。有什么建议么?我尝试过更改函数和形式的位置,以及调整输入字符串上的“”。

<!DOCTYPE html>
    <html>
    <script>
    < head >
    script language = "JavaScript" >

    function Mission() {

        var a1 = document.AgentID.elements[0].value;
        var a2 = document.City.elements[0].value;
        var a3 = document.Country.elements[0].value;
        var a4 = document.Utensil.elements[0].value;
        var a5 = document.Adj1.elements[0].value;
        var a6 = document.Animal.elements[0].value;
        var a7 = document.Transportation.elements[0].value;

        document.write("<br>" + "<br>")
        document.write("Congradulations on accepting your next assignment Agent " + a1 + "")
        document.write("<br>" + "<br>")
        document.write("Your flight leaves to " + a2 + " , " + a3 + " in the next eight hours. You have been granted your weapon of choice, the " + a5 + " " + a4 + ". Your assignment is to capture the " + a6 + " with minimal casualties. Your extraction via " + a7 + " will be waiting.")
        document.write("<br>" + "<br>")
        document.write("Best of Luck Agent " + a1 + "")
        document.write("<br>")
        document.write("Operations HQ")
    }
    </script>

    </head>

    <body>
    <form id="AgentID" name="AgentID">
    AgentID <input type="text">
    </form>
    <form id="City" name="City">
    City <input type="text">
    </form>
    <form id="Country" name="Country">
    Country <input type="text">
    </form>
    <form id="Utensil" name="Utensil">
    Noun <input type="text">
    </form>
    <form id="Adj1" name="Adj1">
    Adjective <input type="text">
    </form>
    <form id="Animal" name="Animal">
    Animal <input type="text">
    </form>
    <form id="Transportaion" name="Transportaion">
    Transportation <input type="text">
    </form>
    <form>

    <input type="button" value="Accept" onClick="Mission()">

    </form>
    </body>
    </html>

最佳答案

您的代码中有很多问题。

  1. HTML 无效。标签不缺少结构。 Script 需要位于 headbody 中。
  2. 您在 JavaScript 中使用的 Transportation 名称与在 HTML 中使用的名称不同。

以下应该有效。

<!DOCTYPE html>
    <html>
	<head>
  
    
    <script type="text/javascript">

    function Mission() {

        var a1 = document.AgentID.elements[0].value;
        var a2 = document.City.elements[0].value;
        var a3 = document.Country.elements[0].value;
        var a4 = document.Utensil.elements[0].value;
        var a5 = document.Adj1.elements[0].value;
        var a6 = document.Animal.elements[0].value;
        var a7 = document.Transportation.elements[0].value;

        document.write("<br>" + "<br>")
        document.write("Congradulations on accepting your next assignment Agent " + a1 + "")
        document.write("<br>" + "<br>")
        document.write("Your flight leaves to " + a2 + " , " + a3 + " in the next eight hours. You have been granted your weapon of choice, the " + a5 + " " + a4 + ". Your assignment is to capture the " + a6 + " with minimal casualties. Your extraction via " + a7 + " will be waiting.")
        document.write("<br>" + "<br>")
        document.write("Best of Luck Agent " + a1 + "")
        document.write("<br>")
        document.write("Operations HQ")
    }
    </script>

    </head>

    <body>
    <form id="AgentID" name="AgentID">
    AgentID <input type="text">
    </form>
    <form id="City" name="City">
    City <input type="text">
    </form>
    <form id="Country" name="Country">
    Country <input type="text">
    </form>
    <form id="Utensil" name="Utensil">
    Noun <input type="text">
    </form>
    <form id="Adj1" name="Adj1">
    Adjective <input type="text">
    </form>
    <form id="Animal" name="Animal">
    Animal <input type="text">
    </form>
    <form id="Transportation" name="Transportation">
    Transportation <input type="text">
    </form>
    <form>

    <input type="button" value="Accept" onClick="Mission()">

    </form>
    </body>
    </html>

关于javascript - 单击时未调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41727630/

相关文章:

javascript - R Shiny 中的搜索框

javascript - 如何定位div

javascript - 我无法在函数中显示跨度 - javascript

javascript - 如果 adblock 阻止了 adsense,则显示图像

javascript - 对象的方法可以作用于自身吗?

javascript - 在 Javascript 中添加两个变量

javascript - css中带空格和不带空格的类有什么区别,多个不带空格的类有什么用

javascript - 无法读取 Node.js 中的 JSON 对象值?

javascript - Velocity:如何将txt内容设置为变量

javascript - 最好使用 JavaScript 还是 XQuery?