HTML和Javascript:表单不会提交。

时间:2015-11-13 02:52:46

标签: javascript html

所以我有一个非常基本的任务完成,它是帮助我们学习一些非常基本的JavaScript。我们必须制作一个表单,当您单击一个按钮时,它会预先形成一个javascript函数。我无法弄清楚为什么它不能完成这个功能。有人可以解决我的代码吗?

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8"/>
		<title>Order Page</title>
	</head>
	<body>
		<form>
			<label>First Name:<input type="text" id="fname" size="30" /><br><br>
			<label>Last Name:<input type="text" id="lname" size="30" /><br><br>
			<label>Number of Widgets:<input type="text" id="num" size="4" /><br><br>
			<button onclick="myFunction()">Send</button>
		</form>



	<script>
	function myFunction() {
		var fname;
		var lname;
		var num;
		lname = document.getElementById("lname").value;
		fname = document.getElementById("fname").value;
		num = document.getElementById("num").value;
		alert("Thanks," + " " + fname + " " + lname + "for ordering" + num + " " + Widgets)

	}
	</script>

	</body>
</html>

5 个答案:

答案 0 :(得分:1)

你应该像Widget那样引用Widget:

alert("Thanks," + " " + fname + " " + lname + "for ordering" + num + " " + "Widgets");

你也可以放';'最后也是。

快速提示,您可以通过打开开发控制台轻松捕获此错误

在谷歌浏览器中按[Ctrl + Shift + J],您将看到错误显示。

希望这有帮助。

答案 1 :(得分:0)

使用此代码再次试用!

    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>Order Page</title>
        <script type="text/javascript">

    function myFunction() {

        var fname;
        var lname;
        var num;
        lname = document.getElementById("lname").value;
        fname = document.getElementById("fname").value;
        num = document.getElementById("num").value;

        alert("Thanks, " +  fname + " " + lname + "for ordering" + num + " " + "Widgets")

    }
    </script>
    </head>
    <body>
        <form action="" name="from">
            <label>First Name:<input type="text" id="fname" size="30" /><br><br>
            <label>Last Name:<input type="text" id="lname" size="30" /><br><br>
            <label>Number of Widgets:<input type="text" id="num" size="4" /><br><br>
              <input type="button" onclick="myFunction()" value="Trial" />

        </form>





    </body>
</html>

答案 2 :(得分:0)

你试过了吗?

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8"/>
		<title>Order Page</title>
	</head>
	<body>
		<form>
			<label>First Name:</label><input type="text" id="fname" size="30" /><br><br>
			<label>Last Name:</label><input type="text" id="lname" size="30" /><br><br>
			<label>Number of Widgets:</label><input type="text" id="num" size="4" /><br><br>
			<input type="button" onclick="myFunction()" value="Send" />
		</form>



	<script>
	function myFunction() {
		var fname;
		var lname;
		var num;
		lname = document.getElementById("lname").value;
		fname = document.getElementById("fname").value;
		num = document.getElementById("num").value;
		alert("Thanks," + " " + fname + " " + lname + "for ordering" + num + " Widgets");
		document.document.getElementsByTagName("form")[0].submit();
	}
	</script>

	</body>
</html>

答案 3 :(得分:0)

您将窗口小部件定义为变量,但假设它是一个字符串。

function myFunction() {
        var fname;
        var lname;
        var num;
        lname = document.getElementById("lname").value;
        fname = document.getElementById("fname").value;
        num = document.getElementById("num").value;
        alert("Thanks, "+fname + " " + lname + " for ordering " + num + " Widgets ")

}

你还没有提到button类型,所以添加了它。

<form>
    <label>First Name:<input type="text" id="fname" size="30" /><br><br>
    <label>Last Name:<input type="text" id="lname" size="30" /><br><br>
    <label>Number of Widgets:<input type="text" id="num" size="4" /><br><br>
    <button onclick="myFunction()" type ="button">Send</button>
</form>

请查看此JSFIDDLE进行演示。 希望这很有用

答案 4 :(得分:0)

var btn = document.getElementById("btn");
btn.onclick = function() {
        var fname;
        var lname;
        var num;
        lname = document.getElementById("lname").value;
        fname = document.getElementById("fname").value;
        num = document.getElementById("num").value;

        alert("Thanks, " +  fname + " " + lname + "for ordering" + num + " " + "Widgets")
};
<form onsubmit="return false;">
	<label>First Name:<input type="text" id="fname" size="30" />
    <br/><br/>
	<label>Last Name:<input type="text" id="lname" size="30" />
    <br/><br/>
	<label>Number of Widgets:<input type="text" id="num" size="4" />
    <br/><br/>
	<button id="btn">Send</button>
</form>