简体   繁体   English

onblur 输入 jquery 时的事件

[英]Event when onblur input jquery

I have an input text (adress) wich take results from Google Maps Autocomplete.我有一个输入文本(地址),它从 Google 地图自动完成中获取结果。 After a click on result, the result appear on the input text and display submit button.单击结果后,结果出现在输入文本上并显示提交按钮。 I would like, if we click one more time on the input text : - erase the result from google (to start on new research) - hide the submit button.我想,如果我们在输入文本上再点击一次: - 从谷歌中删除结果(开始新的研究) - 隐藏提交按钮。

But my script doesn't works well.但是我的脚本效果不佳。

Jquery code :查询代码:

$("#adress").blur(function() {
              if (!this.value) {
             // Hide submit button
             $('#button_submit').css('display', 'none');
             }
       });

My html code :我的 html 代码:

<input type="text" id="adress" value="" placeholder="Please enter adress" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}">

An exemple on jsfiddel : jsfiddle.net/np1wkh9p/12 jsfiddel 的一个例子: jsfiddle.net/np1wkh9p/12

An idea to help me ?一个想法来帮助我?

Thank you !谢谢 !

you forget the # before the id name and for it to work while he click in and out of the text you have to use on focus and blur like that您忘记了 id 名称之前的 # 并让它在他单击进出文本时起作用,您必须像那样聚焦和模糊

$("#address").on('blur focus',function() {
// ^ here
  if (!this.value) {
    // Hide submit button
    $('#button_submit').css('display', 'none');
  }
});

see it here http://jsfiddle.net/np1wkh9p/18/在这里看到它http://jsfiddle.net/np1wkh9p/18/

try giving id before the address尝试在地址之前给出 id

$("#adress").on(function() {
   if (!this.value) {
     // Hide submit button
     $('#button_submit').css('display', 'none');
   }
});

This should work for you.这应该对你有用。

You need a # for ID and no need for inline script:你需要一个 # 作为 ID 而不需要内联脚本:

$(function() {
  $("#adress").blur(function() {
    if (!this.value) {
     $('#button_submit').hide();
    }
  })
  .focus(function() {
    this.value ="";
    $('#button_submit').show();
  });
 });

using使用

<input type="text" id="adress" value="" placeholder="Please enter adress" />

The solution I've found :我找到的解决方案:

$("#address").on('blur focus', function() {
    if (this.value=='') {
    $('#button_submit').css('display', 'none');         
     }
});

http://jsfiddle.net/np1wkh9p/17/ http://jsfiddle.net/np1wkh9p/17/

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

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