我有一個表單,我想驗證,然後如果有效,運行JavaScript函數。對於這一點,我有:HTML5 type =按鈕有效,type = submit not?
function updateMap() {
//dummy
}
<form>
<div class="group">
<input type="number" id="hour" min="0" max="23" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Hour of the day (0-23)</label>
</div>
<div class="group">
<select class="weekselection" id="weekday" required>
<option value="" disabled selected>Choose the weekday</option>
\t \t \t <option value="0">Monday</option>
\t \t \t <option value="1">Tuesday</option>
\t \t \t <option value="2">Wednesday</option>
\t \t \t <option value="3">Thursday</option>
\t \t \t <option value="4">Friday</option>
\t \t \t <option value="5">Saturday</option>
\t \t \t <option value="6">Sunday</option>
\t \t </select>
</div>
<div class="group">
<select class="methodelection" id="normalization" required>
\t \t \t <option value="" disabled selected>Choose data handling</option>
\t \t \t <option value="0">Normalized data</option>
\t \t \t <option value="1">Unnormalized data</option>
\t \t \t <option hidden value="2">Normalization not needed in baseline</option>
\t \t </select>
</div>
<div class="group">
<select class="methodelection" id="method" onchange="setNormSelection()">
\t \t \t <option value="" disabled selected>Choose the method</option>
\t \t \t <option value="0">Baseline (daily/hourly mean)</option>
\t \t \t <option value="1">SGD Regressor</option>
\t \t \t <option value="2">Decision Tree</option>
\t \t \t <option value="3">Neural Network - Multi-Layer Perceptron</option>
\t \t </select>
</div>
<button type=button onClick="updateMap()">Show prediction!</button>
</form>
這工作,但我想真正把該領域的有效性。如果在這段代碼中,我正在練習<button type=submit onSubmit="updateMap()">Show prediction!</button>
,驗證工作正常,但如果填充字段,則updateMap()
函數不會被調用,頁面只會閃爍,就好像它已更新爲默認值一樣。我錯過了什麼?
如果你的事件處理程序沒有做任何事情來阻止「提交」按鈕的天然瀏覽器行爲,表單將被提交。 – Pointy
好的,我的主要目標是獲得與'onclick = updateMap()'相同的功能,但是需要HTML5表單驗證。我該怎麼辦? –