2

解決:

Shmiddty のおかげで、次のことがわかりました。

$( static parent element ).on('submit', '#add_client', function(e) {
    e.preventDefault();
    firm.addUser( $(this), '/ci/firm/add_client', 'client' );
});

説明:

クライアントのためにいくつかのフォームを作成しています。彼がクリックしたリンクに応じて、このフォームを動的に作成したいと考えています。このフォームには、いくつかのデータが自動入力されます。

jQuery動的コンテンツを作成する は次のとおりです。

$('.create').on('click', function(e) {
    e.preventDefault();
    utility.create_modal(); // dynamic div with form
});

これは、div を作成し、PHPフォームを に配置する関数htmlです。

$(document.createElement('div')).attr({
    'class' : 'span3'
}).html( create_div( '/ci/firm/return_user_form/client', 'html' ) ),

ajax関数は次のとおりです。

var result = '';
$.ajax({
    url: path, 
    type: 'get', 
     dataType: type,
     async : false,
     success: function(data) {
         result = data;
     } 
    });
return result;

これは、ファイルから取得された htmlPHPです: (これは巨大なフォームです。問題のボタンを含めるだけです)。

<input type="submit" name="submit" value="Add Client"  id="add_client">

問題:

この動的htmlコンテンツにはフォームがあります。JavaScript含めたフォームにイベントを配置したい。これは可能ですか?もしそうなら、どうすればいいですか?

これは機能しません (#add_clientフォーム内のボタンの ID です):

$('#add_client').on('click', function(e) {
    e.preventDefault();
});
4

1 に答える 1

2
$('some_parent_selector').on('click', '#add_client', function(e) {
    e.preventDefault();
});

The on listener needs a parent object (that is not dynamic) to listen for a click event that bubbles up. Then, when the event bubbles up to the parent, it determines whether or not it originated from '#add_client', and if it does, it calls your anonymous function.

于 2012-08-29T18:31:33.030 に答える