简体   繁体   English

javascript / jquery-检查input [type ='search']的焦点?

[英]javascript/jquery - check focus of input[type='search']?

I'm trying this: 我正在尝试:

$("input[type='text'], input[type='search']").on("focus", function(e, ui) {
    $("[data-role='footer']").hide();
    console.log('focus');
});

I'd expect it to log when I focus on a search field but that seems to not be the case. 我希望当我专注于搜索字段时,它会记录下来,但事实并非如此。 Works fine when I focus on a text field. 当我专注于文本字段时,效果很好。 I'm fairly new to javascript/jquery so maybe I'm just being dumb about something. 我对javascript / jquery还是比较陌生,所以也许我只是对某事感到愚蠢。 Did some Googling but didn't find anything helpful. 做了一些谷歌搜索,但没有发现任何帮助。

Here's a fiddle: http://jsfiddle.net/RDz2J/ 这是一个小提琴: http : //jsfiddle.net/RDz2J/

It works, all that you were missing was an extra ) to close the function properly. 它可以正常工作,您所缺少的只是一个额外的) ,可以正常关闭该功能。

It works now and saves Got it in a div, I removed alert as it forced me to close the browser as a whole :( Sorry. 它现在并保存Got it在一个div,我删除警报,因为它迫使我关闭浏览器作为一个整体:(不好意思。

http://jsfiddle.net/afzaal_ahmad_zeeshan/RDz2J/4/ http://jsfiddle.net/afzaal_ahmad_zeeshan/RDz2J/4/

And also try to shorten down the code as: 并尝试将代码缩短为:

$("input[type='text'], input[type='search']").focus(function() {
    $("[data-role='footer']").hide();
    console.log('focus');
});

I have removed the e, ui since you're not using it in the code and have changed the on event to focus directly. 我删除了e, ui因为您没有在代码中使用它,而是将on事件更改为直接focus

You had a simple syntax error; 您有一个简单的语法错误; you had not added an extra end paranthesis. 您还没有添加额外的结束括号。 Here is an updated fiddle, and here is the new code 是更新的小提琴,这是新的代码

$("input[type='search']").on("focus", function (e, ui) {
    alert('got it!');
});

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

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