2017-01-24 31 views
1

私は入力テキストボックスid = "inputChanges"を持っています。ユーザーがテキストボックスをクリックすると、イベントが発生し、 "inputChanges"というIDを取得できます。しかし、私が得たいのは、ユーザーがテキストボックスの外側をクリックした場所のIDです。例えば、私が入力フィールドにいて、変更を加えてボタンを直接クリックすると、ボタンのIDが必要になります。jquery changeイベントIDにフォーカスが失われました。

それが正しいIDを取得しないよう、次は、私のために動作しません:

$(document).ready(function(event) { 
    $("#inputChanges").change(function(event) { 
     alert(event.target.id); 
    }); 
}); 
+0

あなたがクリックされた要素のidを取得したい場合、あなたはその要素 –

+0

のclickイベントをリッスンする必要があるので、あなたはすべきではありませんボタンのクリックを聞いて? – epascarello

答えて

0

ではなく、ボタンにイベントリスナーを追加します。

$("#button").click(function(e) { 
    alert(this.id); 
}); 
0

は、このようなclickイベントを聞きます。

$(document).ready(function(event) { 
 
$("#inputChanges").click(function(event) { 
 
    alert(event.target.id); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="inputChanges">

0

$(document).ready(function(event) { 
 
    var focused = false; 
 
    $("#inputChanges").focus(function(event) { 
 
     focused = true; 
 
    }); 
 
    $(document).click(function(event){ 
 
     var target = event.target; 
 
     if(focused && target.id != "inputChanges"){ 
 
      focused = false; 
 
      alert(target.id); 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="inputChanges"/> 
 
<p id="paragraph">this is a paragraph</p> 
 
<div id="div">this is a div</div>

関連する問題