- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Recursive call when post request</title>
- <script type="text/javascript" src="jquery-1.5.1.js"></script>
- <script type="text/javascript">
- $(function() {
- var count = 0;
- $('input:button').click(function() {
- // identity
- count++;
- var tick = count;
- // post url
- var url = '${contextPath}/';
- // post param
- var param = $(this).data();
- console.log(param);
- $('<div>(' + tick + ') Post to : ' + url + '</div>').appendTo(document.body);
- $.post(url, param, function(data) {
- $('<div style="color:blue;">(' + tick + ') Ajax works successfully.</div>').appendTo(document.body);
- });
- });
- });
- </script>
- </head>
- <body>
- <input type="button" value="click to post" data-action="test" data-name="bruce" data-age="31" />
- </body>
- </html>
ボタンをクリックすると、data()のオブジェクトがハンドルを値を持っているため、postするときにハンドルは再び執行することになる。つまり、クリック事件は繰り返しする。この現象を避けるには、ハンドル/イベントを削除しなければならない。