0

次のようなコードがあります。

<div id="container">
// 3 lines other code here
<a hef="#" id="agreelink"><input id="agree" name="agree" value="Agree" style="position:relataive;border-style:none" /></a>
<input id="disagree" name="disagree" value="Disagree" style="position:relataive;border-style:none" />
<input id="abstain" name="abstain" value="Abstain" style="position:relataive;border-style:none" />
// 2 more lines here
</div>

.live()次に、要素が動的であり、火を複製し続けるために使用していた関数があり、に移動する必要があり.on()ます。したがって、これらのボタンのいずれかを起動しようとした関数コードは次のとおりです。

<$('#container').on('click', 'input', function() {
alert($(this).attr('name'));
});

<$('#container').on('click', '#agree', function() {
alert($(this).attr('name'));
});

<$('#container').on('click', 'a', function() {
alert($(this).attr('id'));
});

しかし、私はそれを発火させることができません。オンラインで検索して試してみましたが、何か正しいことをしていないと確信しています。誰かが私が間違っている場所を提案できますか?

ありがとう

4

2 に答える 2

1

これを試すこともできます:http://jsfiddle.net/VVfUK/live()これは最初の選択肢だったので、イベントのバブルe.stopPropagation()を防ぐために使用し ました。click

それが役に立てば幸い :-)

于 2012-10-20T20:03:45.410 に答える
1

それらを包み込んでみてください$(document).ready()

$(document).ready(function(){

    $('#container').on('click', 'input', function() 
    { 
       alert($(this).attr('id')); 
    });

});
于 2012-10-20T19:58:15.323 に答える