简体   繁体   English

防止输入字段上的点击事件

[英]Prevent click-Event on input field

I am developing a PhoneGap-App for iPad.我正在为 iPad 开发 PhoneGap-App。 On one screen you have to fill out a form with about 20 textfields.在一个屏幕上,您必须填写一个包含大约 20 个文本字段的表单。 As input-fields only react to the click-event (which have this not long but yet annoying delay) I tried the following:由于输入字段只对点击事件做出反应(这不长但很烦人的延迟),我尝试了以下操作:

$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("touchend", function(e) {
    if(!swipe) {
        $(this).focus();
    }
    swipe = false;
    return false;
});

(I check for swipe in the touchmove event) (我在touchmove事件中检查滑动)

This works, but now I want to prevent the original click event on the inputs.这有效,但现在我想阻止输入上的原始点击事件。 The problem is, when I activate an input-field with the .focus() method, the keyboard pops up and slides the page a little bit up AND then the click event gets fired and activates another input-field a little bit below my desired input.问题是,当我使用.focus()方法激活输入字段时,键盘会弹出并将页面向上滑动一点,然后click事件被触发并激活另一个比我想要的输入字段低一点的输入字段输入。

For preventing click I already tried:为了防止点击,我已经尝试过:

$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(e) {
    return false;
});

but this also doesn't work :(但这也不起作用:(

Is there another trick to activate input-fields immediately after I touched it without any delay?是否有其他技巧可以在我毫不拖延地触摸它后立即激活输入字段?

you can try this你可以试试这个

 $('input, textarea, select').click(function(event) {
    event.preventDefault();
 });

You need to use preventDefault to prevent the default action:您需要使用 preventDefault 来阻止默认操作:

  $('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(e) {
        e.preventDefault();
    });

Documentation : http://api.jquery.com/event.preventDefault/文档: http : //api.jquery.com/event.preventDefault/

It is stop propagation.它是停止传播。

$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(event) {
        event.stopPropagation();
    });

I had this same issue as you and tried to implement all of the other solutions posted and found that none of them completely worked for me.我和你有同样的问题,并试图实施所有其他发布的解决方案,但发现它们中没有一个完全适合我。 Instead, I borrowed ideas from all of the prior solutions and found that this snippet of code solved my problem.相反,我借鉴了之前所有解决方案的想法,发现这段代码解决了我的问题。

    $('input, textarea, select').click(function (event) {
        event.stopPropagation();
    });

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

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