简体   繁体   English

仅通过单击表单提交表单,而不是使用 javascript 的 from 字段

[英]submit form only by click on form and not the from fields using javascript

 document.getElementById("searhForm").onclick = function() { alert(); //document.getElementById("searhForm").submit(); }
 #searhForm { border:1px solid red; }
 <form method="post" action="#" id="searhForm" > <input class="form-control" type="search" id="query" placeholder="Search!" > </form>

I want to trigger alert() when user click anywhere on the form.当用户单击表单上的任意位置时,我想触发警报()。 but when user click the text-field it should simply let the user write and alert() should not be triggered但是当用户单击文本字段时,它应该让用户编写并且不应该触发 alert()

Check for the nodeName before triggering the alert在触发警报之前检查nodeName

 document.getElementById("searhForm").onclick = function (event) { if (event.target.nodeName;== "INPUT") { alert(); } }
 #searhForm { border: 1px solid red; }
 <form method="post" action="#" id="searhForm"> <input class="form-control" type="search" id="query" placeholder="Search!"> </form>

Check it.核实。 If it doesn't work, let me know.如果它不起作用,请告诉我。

document.getElementById("searhForm").addEventListener("click",function(e) {
var a = document.getElementById("query");
if(e.target != a) {
        alert();
        //document.getElementById("searhForm").submit();
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM