任何人都可以告诉我如何在提交按钮
上触发unpress事件如果任何字段为空,我正在按下按钮进行验证。我阻止导航到第二页。但验证后仍然按下“提交”按钮,如果“从”或“到”字段为空。 在这里输入代码
<form action="NewFile.html">
From:<input id="from" type="text" name="From-busstop">
To:<input id= "to" type="text" name="To-busstop">
How do you want to travel?:<Select id="how" name="How">
<option>Minimum Number of Hops</option>
<option>Maximum Bus Route Availability</option>
<option>Via Terminal Bus Stations Only</option>
<option>Direct Routes Only</option>
<option>Shortest Distance</option>
</Select>
<p>
<a href="#two" id="submit" data-direction="reverse" data-role="button" data-transition="flip" onClick="loadpage()"
>Submit"</a>
</p>
</form>
<script type="text/javascript">
$('#submit').on('click',function(e) {
var from = document.getElementById('from').value;
var to = document.getElementById('to').value;
var how = document.getElementById('how').value;
if(from.length < 1)
{
alert("From bus-stop is empty");
document.getElementById("from").focus();
e.preventDefault();
}
else if(to.length < 1)
{
alert("To bus-stop is empty");
document.getElementById("to").focus();
e.preventDefault();
}
});
</script>
答案 0 :(得分:0)
更改
else if(to.length < 1)
仅限
if(to.length < 1)
你想做两个if条件,不是吗?
如果是,则不需要 else