我怎么能否认在特殊项目上被触发的事件

时间:2014-02-03 17:22:37

标签: jquery

我有一些带有输入的div。现在我想在每次输入发生变化时发起事件。到目前为止一切都很好:)但如果我选择第一个输入的文件,我不希望事件发生。

see my Fiddle

这是源

HTML:

<div class="someForm">
<input type="file" name="logo" class="logoselector" />
<br />
<div class="row-fluid">
    <input type="text" name="firmenname" />
    <br />
    <input type="text" name="adresse" />
    <br />
    <input type="text" name="telefon" />
    <br />
    <input type="text" name="fax" />
</div>
</div>

这是jquery

$('.someForm').on('change', function(){
        alert('changed');
});

我已经尝试了一些结构:not(...)但没有结果:/

5 个答案:

答案 0 :(得分:1)

使用:not():first

尝试此操作
$('.someForm input:not(:first)').on('change', function(){
        alert('changed');
});

这样jQuery将查找所有输入,但不会查找第一个::not(:first)

Demo


您还可以使用:file

中的:not()来定位file类型

像:

$('.someForm input:not(:file)').on('change', function() {
    alert('changed');
});

Demo

答案 1 :(得分:1)

添加:not:first

$('.someForm input:not(":first")').on('change', function(){

<强> jsFiddle example

答案 2 :(得分:1)

您可以在.on()中添加选择器作为第二个参数,以排除type='file'的输入:

$('.someForm').on('change', ':input[type!="file"]', function() {
    alert('changed');
});

答案 3 :(得分:0)

您可以像这样更改选择器:

$('.someForm :not(:file)').on('change', function() {
    alert('changed');
});

<强> Demon

答案 4 :(得分:0)

您可以使用e.target.className

$('.someForm').on('change', function(e){
    if (e.target.className != 'logoselector') {
        alert('changed');
    }
});