簡體   English   中英

擁有一類輸入,並且需要在更改之后知道更改了哪些輸入

[英]Have a class of inputs, and need to know what input has been changed, after it has been changed

我有一類投入。 我希望能夠檢測到哪些特定輸入已更改。 我首先檢測到類中的更改,然后我想知道什么輸入已更改。

<input type="text" name="address" id="address" class="className"/>
<input type="text" name="city" id="city" class="className"/>
<input type="text" name="country" id="country" class="className"/>
<script>
$('.className').keyup(function(){
  //Want to say 'You changed '(the id of the input changed)'
});
</script>

使用this.id獲取當前元素的id

 $('.className').keyup(function(){ console.log('You changed '+this.id); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="address" id="address" class="className"/> <input type="text" name="city" id="city" class="className"/> <input type="text" name="country" id="country" class="className"/> 

所有DOM事件處理程序都會自動傳遞對Event對象的引用,該對象表示觸發該函數的事件。 您可以使用此對象的.target屬性訪問負責觸發事件的DOM對象。

您也可以使用this代替event.target ,但這僅在不使用事件委托(冒泡)的情況下才是准確的。

 $('.className').keyup(function(event){ console.log("The instigator was: " + event.target.toString() + " with an id of: " + event.target.id); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="address" id="address" class="className"/> <input type="text" name="city" id="city" class="className"/> <input type="text" name="country" id="country" class="className"/> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM