只需点击表单提交表单,而不是使用javascript的from字段



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>

当用户点击表单上的任何位置时,我想触发alert((。但当用户点击文本字段时,它应该只让用户写,而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>

检查一下。如果不起作用,请告诉我。

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

最新更新