使用jQuery搜索时停止提交表单 [英] Stop form from submitting when searching with jQuery

查看:67
本文介绍了使用jQuery搜索时停止提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上输入了Google Map搜索.如果用户键入地址并按Enter,则将提交整个表单,而不是搜索地图.我正在使用下面的代码来防止这种情况:

I have a google map search input on my form. If the user types an address and hits enter, the entire form is submitted rather than searching the map. I'm using the code below to prevent this:

jQuery('#gform_6').submit(function() {
    // check if address field is empty or not
   if(jQuery("#map_search").val()=="") // empty
      return true; // ok submit form
   else { // address filled
      ChangeMap();
      jQuery("#map_search").val("");
      return false;
    }
});

但是,我可以一次使用此功能.如果我尝试再次搜索并按Enter键,则什么也不会发生.是因为我在Submit函数上返回"false"吗?

However, I can use this feature one time. If I try to search again and hit enter, nothing happens. Is it because I'm returning "false" on the submit function?

推荐答案

我相信您正在寻找 event.preventDefault()

event.preventDefault:如果调用此方法,则不会触发事件的默认操作.

event.preventDefault : If this method is called, the default action of the event will not be triggered.

jQuery('#gform_6').submit(function(event) {
  event.preventDefault();
  // check if address field is empty or not
  if(jQuery("#map_search").val()==""){ // empty
     return true; // ok submit form
  }else { // address filled
    ChangeMap();
    jQuery("#map_search").val("");
    return false;
  }

这篇关于使用jQuery搜索时停止提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆