防止在输入字段上单击事件

Prevent click-Event on input field

本文关键字:单击 事件 字段 输入      更新时间:2023-09-26

我正在为iPad开发PhoneGap应用程序。在一个屏幕上,你必须填写一个包含大约20个文本字段的表格。由于输入字段只对点击事件做出反应(点击事件的延迟不长,但很烦人),我尝试了以下操作:

$('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;
});

(我在touchmove事件中检查滑动)

这是可行的,但现在我想阻止输入上的原始单击事件。问题是,当我用.focus()方法激活一个输入字段时,键盘弹出并将页面向上滑动一点,然后click事件被触发,并激活另一个比我想要的输入低一点的输入字段。

为了防止点击,我已经尝试过:

$('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;
});

但这也不起作用:(

有没有其他技巧可以在我触摸后立即激活输入字段而不延迟?

你可以试试这个

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

您需要使用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();
    });

文件:http://api.jquery.com/event.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(event) {
        event.stopPropagation();
    });

我和您遇到了同样的问题,并试图实现发布的所有其他解决方案,但发现它们都不完全适用于我。相反,我借鉴了以前所有解决方案的思想,发现这段代码解决了我的问题。

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