2016-11-12 25 views
0

對於可能是noob的問題感到抱歉..我一直在擺弄此表單一段時間,但似乎無法使其工作。顏色的變化工作,但提交按鈕根本不會導致鏈接..無法獲取此表單以適用於HTML5

<center> 
    <form name="form1" method="POST"> 
     <input id="codebox1" type="text" onchange="checkFilled1();" /> 
     <input id="codebox2" type="text" onchange="checkFilled2();" /> 
     <input id="codebox3" type="text" onchange="checkFilled3();" /> 
     <br> 
     <br> 
     <input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" /> 
    </form> 
</center> 

這裏的功能是:

<script> 
function checkFilled1() { 
    var inputVal = document.getElementById("codebox1"); 
    if (inputVal.value == "1234") { 
     inputVal.style.backgroundColor = "lightgreen"; 
    } else { 
     inputVal.style.backgroundColor = "red"; 
    } 
} 
checkFilled1(); 
</script> 

<script> 
function checkFilled2() { 
    var inputVal = document.getElementById("codebox2"); 
    if (inputVal.value == "1234") { 
     inputVal.style.backgroundColor = "lightgreen"; 
    } else { 
     inputVal.style.backgroundColor = "red"; 
    } 
} 

checkFilled2(); 
</script> 

<script> 
function checkFilled3() { 
    var inputVal = document.getElementById("codebox3"); 
    if (inputVal.value == "1234") { 
     inputVal.style.backgroundColor = "lightgreen"; 
    } else { 
     inputVal.style.backgroundColor = "red"; 
    } 
} 

checkFilled3(); 
</script> 

<script> 
function testResults() { 
    var inputVal1 = document.getElementById("codebox1"); 
    var inputVal2 = document.getElementById("codebox2"); 
    var inputVal3 = document.getElementById("codebox3"); 
    if (inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234") { 
     window.open("http://google.com", "_parent"); 
    } else { 

    } 
} 

testResults(); 
</script> 

感謝您的幫助!

+1

擺脫內嵌的JavaScript和所有移動到它自己的文件。所有對函數的內聯調用都可以通過簡單地向項目添加一個id並在Javascript文件中引用它來完成。 –

+0

@MichaelMcCoy你能詳細說說你對內聯函數調用的看法嗎?任何教程或MDN文檔都會更好! – asgs

回答

0

更改此:

<form name="form1" method="POST"> 

[...] 

<input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" /> 

這樣:

<form name="form1" onSubmit="testResults()" method="POST"> 

[...] 

<input id="button1" type="submit" value="Submit" /> 

===

重構版本

var codeboxes = document.getElementsByClassName('codebox'); 
 

 
function testResults() { 
 

 
    var correctValues = 0; 
 

 
    for (var i = 0; i < codeboxes.length; i++) { 
 
     if (codeboxes[i].value === '1234') { 
 
      correctValues++; 
 
     } 
 
    } 
 

 
    if (correctValues === codeboxes.length) { 
 
    \t window.alert('You have entered all ' + codeboxes.length + ' values correctly.'); 
 
    } 
 

 
    else { 
 
\t window.alert('You have not entered all ' + codeboxes.length + ' values correctly.'); 
 
    } 
 
}
<form name="form1" onSubmit="testResults()" method="POST"> 
 
<input class="codebox" type="text" /> 
 
<input class="codebox" type="text" /> 
 
<input class="codebox" type="text" /> 
 
<input id="button1" type="submit" value="Submit" /> 
 
</form>

+0

感謝您的幫助,Rounin!但它仍然不起作用..這個錯誤出現,而不是形式: \t 此XML文件似乎沒有任何樣式信息與它關聯。 文檔樹如下所示。 InvalidArgument 無效的參數。

POST對象期望的內容類型的multipart/form-data的
Lukasj10

+0

更多是出於好奇比什麼都重要,我已經加入我自己的「重構版」上述職位。據我所知,它工作正常。 – Rounin

+1

謝謝,Rounin!您的所有建議都會使其脫機工作。必須是特定模板(HTML5)的問題我試圖將其納入.. – Lukasj10

1

您正在調用方法testResults,它不接受任何參數。將提交按鈕標記更改爲以下,並調用testResults方法。

<input id="button1" type="submit" value="Submit" onClick="testResults()" /> 

沒有參數。

0

您在JavaScript中有語法錯誤。打開你的瀏覽器工具的控制檯,看看:

Unexpected token &&

您需要整個包住,如果條件括號,就像這樣:

if ((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234")) { 
window.open("http://www.example.com", "_parent"); 

那說,這不會在提交數據表格。只要條件成立,它就會打開提供的URL - 這就是你在問題中提出的問題。

0

function checkFilled(inp) { 
 
    var inputVal = inp; 
 
    if (inputVal.value == "1234") { 
 
    inputVal.style.backgroundColor = "lightgreen"; 
 
    } else { 
 
    inputVal.style.backgroundColor = "red"; 
 
    } 
 
} 
 

 
function testResults() { 
 
    var inputVal1 = document.getElementById("codebox1"); 
 
    var inputVal2 = document.getElementById("codebox2"); 
 
    var inputVal3 = document.getElementById("codebox3"); 
 
    if ((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234")) { 
 
    alert('Here comes window opening...'); 
 
    //window.open("http://google.com", "_blank"); 
 
    } else { 
 
    alert('Error ...'); 
 
    } 
 
} 
 

 
//testResults();
<form name="form1" method="POST"> 
 
    <input id="codebox1" type="text" onchange="checkFilled(this);" /> 
 
    <input id="codebox2" type="text" onchange="checkFilled(this);" /> 
 
    <input id="codebox3" type="text" onchange="checkFilled(this);" /> 
 
    <br> 
 
    <br> 
 
    <input id="button1" type="submit" value="Submit" onClick="testResults(this.form)" /> 
 
</form>

你做了一個錯誤的testResults()函數,此時如果......看到的片段(一點點optimalization)

+0

感謝您的輸入!儘管如此,還是沒有。我收到此錯誤: 此XML文件似乎沒有任何與其關聯的樣式信息。文檔樹如下所示。 InvalidArgument參數無效。

POST對象期望Content-Type multipart/form-data
Lukasj10

+0

@ Lukasj10: 1.你需要發佈你的數據嗎?如果沒有,請將您的提交按鈕寫入常規按鈕,並且不要寫任何方法來形成開放標籤。 2.有你的html文件正確的擴展名和正確的頭文件(像:<!DOCTYPE html>)? –

0

編輯您的表單標籤缺失()下面,

<form name="form1" method="POST" onSubmit="testResults()"> 

編輯您的提交按鈕,如下,

<input id="button1" type="submit" value="Submit"/> 

編輯在if語句如下,

if((inputVal1.value == "1234") && (inputVal2.value == "1234") && (inputVal3.value == "1234")) 
{ 
    window.open("http://www.google.com", "_parent"); 
}