Take a small example to illustrate the situation
?
| 1 2 3 4 5 6 7 8 |
<script src="jquery-1.11.0.min.js"></script><input type="button" value="点击我" id="k" /> <script>$(‘#k‘).click(function() { alert("kkkkk");})</script> |
If you write this directly on the page, you can pop up "kkkkk". However, when the input element is dynamically loaded and output to the page by Ajax, the popup window is invalidated, which is equivalent to the Click event not being bound to the element with ID K.
This can be written in script:
?
| 1 2 3 |
$(document).on(‘click‘,‘#k‘,function() { alert("kkkkk");}) |
The usual use of live () or delegate () to replace on () also achieves the desired effect. But note that I use the jquery-1.11.0 version for example, from the beginning of the jquery1.9 version to remove Live (), heard is a performance problem. So the higher version of jquery can be used with delegate () and on ().
jquery binding Dynamic Elements