2014-03-01 25 views
0
獲取表單元素ID

這是一個簡單的表單。我所知道的是,第一個輸入索引= 0,第二個輸入索引= 1,第三個輸入索引= 2.我想用jQuery來(手動)提醒一個輸入元素的ID(例如在mouseenter上,或者按鍵輸入)通過使用其索引。例如,索引1上的mouseenter將爲alert "surname"Jquery從索引

<form> 
    <input type ="text" id ="name"> 
    <input type ="text" id ="surname"> 
    <input type ="password" id ="pass"> 
</form> 

我只需要從索引中檢索Id的語法。

回答

1

您可以使用:

$('input').on('mouseenter keypress', function() { 
    console.log(this.id); // I'm not using alert here since it's too annoying :P 
}) 

Fiddle Demo