myApp.jsが次のように含まれているhtmlページを開きます。
<html>
<body>
......
<div id="dynamicForm"></div>
......
<script type="text/javascript" src="myPath/myApp.js"></script>
......
</body>
</html>
次に、myApp.jsで定義されたフォームをこのhtmlページ( "#dynamicForm")に動的にロードしたいのですが、myApp.jsには次のような行があります。
.....
//load the form on the html page
$('#dynamicForm').html(createDynamicForm()).show();
.....
//the function creating the form
function createDynamicForm(){
var jqForm=$('<form id="dynamicFormId">'
......
+ '<button id="btn">OK</button>'
+ '</form>');
$('body').append(jqForm);
return jqForm;
}
.....
//click the button on the form to trigger the alert box
("#btn").click(function(){
alert("you click the button");
});
フォームをhtmlページにロードできますが、この動的にロードされたフォームで[OK]ボタンをクリックしても、アラートがポップアップしません。これは、動的フォームの前にmyApp.jsにhtmlページがロードされているためと思われます。が作成されると、「クリック」イベントは、フォームを作成するmyApp.jsで定義されたハンドラーでは処理できません。どうすればこの問題を解決できますか?