如何使用Javascript触发按钮取消按下

How to trigger unpress on a button using Javascript

本文关键字:取消 按钮 何使用 Javascript      更新时间:2023-09-26

任何人都可以告诉我如何在提交按钮上触发取消按下事件

如果任何字段为空,我正在对按下按钮进行验证. 我正在阻止导航到第二页。但是,"提交"按钮在验证后仍处于按下状态,如果"发件人"或"收件人"字段留空。 在此处输入代码

        <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)

你想做两个如果条件,不是吗?

如果是,则不需要其他