如何使用Javascript触发按钮按下 [英] How to trigger unpress on a button using Javascript
本文介绍了如何使用Javascript触发按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以告诉我如何触发提交"按钮上的取消按下事件
Can anybody please tell me how to trigger unpress event on Submit button
如果有任何字段为空,我正在按按钮进行验证.我正在阻止导航到第二页.但是,在验证之后以及发件人"或到"字段为空后,提交"按钮仍保持按下状态. 在此处输入代码
I am doing a validation on button press if any of the fields are empty . i am preventing th e navigation to second page. But the Submit button remains pressed after validation and if From or To field is left empty. enter code here
<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>
推荐答案
更改
else if(to.length < 1)
仅
if(to.length < 1)
您想执行两个if条件,不是吗?
You want to do two if-conditions, don't you?
如果是,则不需要 else
这篇关于如何使用Javascript触发按钮按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文