2016-09-24 4 views
0

DOMにボタンを動的に追加しています。各ボタンには、ポップオーバーがあります。ここに私のコードは次のとおりです。 - 私はポップオーバーボタン「50」をクリックすることClickMeボタンのIDを取得したいPopoverボタンのIDを取得する

<button class="pop-Add" id='button1'>Add</button> 

var popOverSettings = { 
placement: 'bottom', 
container: 'body', 
selector: '[rel="popover"]', 
html:true, 
content: "<button type='button' id='+50' class='btn btn-small pop_button'> + 50 </button>" 
} 

$('body').popover(popOverSettings); 
$('.pop-Add').click(function() { 
     var id = Math.floor((Math.random() * 1000000) + 1); 
    button = "<button rel='popover' id='" + id + "'>Click me</button>" 
    $('body').append(button); 
}); 

jsFiddle

。どうすれば入手できますか?

これは私が現在やっている方法である: -

$(document).on('click', '.bid_value_buttons' , function(){ 
    alert($(this).attr('id')) 
} 
+1

を助け、このアプローチでの問題は何であるホープ? – Rajesh

+0

それは私に '+ 50'というIDを与えます。私はむしろ 'Math.random'を使って生成した' ClickMe'ボタンの 'id'を望みます。 – PythonEnthusiast

+0

問題ありません。この視点のガイダンスは? – PythonEnthusiast

答えて

2

私は、コードの下に使用してこれを行っている:

HTML:

<button class="pop-Add">Add</button> 
<script> 
function display(el) { 
     var id = $(el).parent().parent().prev().prop("id"); 
     alert(id); 
    } 
    </script> 

のjQueryコード:

var popOverSettings = { 
    placement: 'bottom', 
    container: 'body', 
    selector: '[rel="popover"]', 
    html:true, 
    content: "<button type='button' id='+50' class='btn btn-small btn-empty bid_value_buttons'> + 50 </button>" 
} 

$('body').popover(popOverSettings); 
$('.pop-Add').click(function() { 
     var id = Math.floor((Math.random() * 1000000) + 1); 
    button = "<button rel='popover' id='" + id + "' OnClick='display(this);'>Click me</button>" 
    $('body').append(button); 
}); 

チェックアウトこれをfiddle

スクリーンショットの下に見つけてください:

enter image description here

は、それはあなたに

おかげ

+0

質問を再度読んでください。彼はオーバーレイの他のボタンをクリックしたときにクリックされたボタンのIDを警告したいと思っています – mplungjan

+0

+1親の関係を把握するのは時期尚早でした。これは、htmlが変更されない場合に機能します。 – mplungjan

+0

親と子の関係が同じであれば、これは動作します。+1ありがとう: –

2

私は最も簡単な答えはちょうど新しく作成されたポップオーバーにdata-attributeとして「私をクリックし、」ボタンのIDを追加することだと思うし、そのdata-attributeアクセス:

JS Fiddle

$(document).on('click', '.click-me-button', function(){ 
    var id = $(this).attr('id'); 
    $('.popover-content').last().attr('data-id', id); // Will always be the last popover (newest in DOM) 
}) 

$(document).on('click', '.popover-content' , function(){ 
    alert($(this).attr('data-id')); 
}); 

をそして、あなたは本当にIDオフに基づいて、ボタンにアクセスする必要がある場合には、Y OUはちょうど行うことができます:

$(document).on('click', '.popover-content' , function(){ 
    var id = $(this).attr('data-id');  
    alert($('#' + id).attr('id')); // easily access the id 
}); 
+0

2つのボタンを追加した後にbutton1をクリックすると、これはbutton2に警告します – mplungjan

関連する問題