我想通過表單在一個頁面上收集數據,然後將該數據傳輸到下一頁以用於JS函數。 (具體來說,我希望用戶輸入二次方程的A,B和C的值,然後發送到腳本採用這些值的頁面並運行公式+輸出答案)。使用表單來收集Javascript函數的數據?
這裏是我的第一頁--->
<body>
<h1> Welcome to the Math Equation Solver </h1>
<p> Which equation would you like to solve? (Simply input the data for the equation you wish to solve). </p>
<form name="quad" method="get" action="JS_B.html">
<input
<input type="text" name="a_val" size="5">
<br>
<input type="text" name="b_val" size="5">
<br>
<input type="text" name="c_val" size="5">
<br>
<input type="submit" method="get" action="JS_B.html" value="Submit">
<input type="hidden" name="a_val">
<input type="hidden" name="b_val">
<input type="hidden" name="c_val">
</form>
下面的代碼是我的第二頁--->
<title>JS_Math Scripts</title>
<body>
<script type="Javascript">
function answer()
{
var a = a_val
document.writeln(a_val)
var b = b_val
var c = c_val
var root = Math.pow(b, 2) - 4 * a * c
var answer1 = (-b + Math.sqrt(root))/2*a
var answer2 = (-b - Math.sqrt(root))/2*a
if(root<'0');
{
alert('This equation has no real solution.')
}else{
if(root=='0')
{
answerOne = answer1
document.writeln(answerOne)
answerTwo = 'No Second Answer'
}else{
answerOne = answer1
document.writeln(answerOne)
answerTwo = answer2
document.writeln(answerTwo)
}
}
}
// End -->
</script>
<input type="hidden" name="a_val">
<input type="hidden" name="b_val">
<input type="hidden" name="c_val">
<input type="hidden" name="answerOne">
<input type="hidden" name="answerTwo">
<input type="hidden" name="Answer">
</body>
</html>
的代碼,所以不管怎麼說,當A I輸入值,B和C會把我帶到第二頁,但我沒有得到結果。我試過檢查元素,控制檯沒有顯示任何錯誤,所以我認爲我的數據傳輸正確。有任何想法嗎?
什麼是預期結果Ø f'var answer1 =(-b + Math.sqrt(root))/ 2 * a;', 'var answer2 =(-b - Math.sqrt(root))/ 2 * a'? – guest271314
可能的重複[如何在JavaScript中獲取查詢字符串值?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – colonelclick