2017-01-07 103 views
0

我有這行代碼$('#tbl-purchase-list').on('keyup change', 'input[type="text"]',檢測所有input[type="text"]字段的輸入。但是,我有一個輸入字段,我不希望在鍵盤上輸入文本時對其進行檢測或對其進行任何更改。 <input type="text" class="form-control purchase-freight-charge" id="purchase-freight-charge" value="" />。有沒有一種方法在javascript中忽略某個元素的某些事件?像這樣的$("#purchase-freight-charge").ignoreEvents();?請幫忙。非常感謝。如何檢測/忽略某個html元素中的javascript事件?

+0

的可能的複製[jQuery的禁用單場規則驗證(http://stackoverflow.com/questions/2853416/jquery-disable-rule-validation -on-a-single-field) – Paul

+3

[':not()'](https://api.jquery.com/not-selector/)選擇器。 –

回答

2

Ofcourse有一個:not()僞選擇:

$('#tbl-purchase-list').on('keyup change', 
          'input[type="text"]:not(#purchase-freight-charge)', 
          ...); 

在組匹配選擇的,不會增加目標元素。因此,:not()內部不存在該元素的任何事件。

事件這也可以做到:

$('#tbl-purchase-list').on('keyup change', 'input[type="text"]',function(ev){ 
    if(this.id === "purchase-freight-charge"){ 
     this.value = ""; 
    } 
}); 
+0

使用event.stopImmediatePropagation();此方法的優點是什麼? – Eli

+0

爲此,您必須綁定選擇器上的另一個事件。在這種情況下,你只需要在一個地方工作。還有一件事你可以做...更新。 – Jai

1

您可以使用jQuery的event.stopImmediatePropagation()

$("#purchase-freight-charge").on('keyup change', function(event) { 
    event.stopImmediatePropagation(); 
}); 

// Future event bindings won't be executed for #purchase-freight-charge 
// ...