admin管理员组文章数量:1428567
I've managed to figure out how to grab external json and use that in a modal using a manually set id in the javascript.
I'm stuck on passing the id from the button to the javascript url. My button markup is as follows;
<button type="button" class="btn btn-primary btn-sm modal-btn" data-id="10">Details</button>
My JS currently looks like this;
<script type="application/javascript">
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'T<"clear">lfrtip',
tableTools: {
"sRowSelect": "multi",
"aButtons": [ "select_all", "select_none" ],
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ 1 ] }
]
}
});
$('#example tbody').on( 'click', 'tr', function (e) {
if($(e.target).is('.modal-btn')){
$('#fullDetails').modal('show');
var event = "details.php?id=" + ($(this).data('id'));
$.getJSON(event, function( data ) {
$(".modal-title").text(data.title);
$(".modal-audience").text(data.audience);
$(".modal-leader").text(data.leader);
$(".modal-date").text(data.date);
$(".modal-start_time").text(data.start_time);
$(".modal-end_time").text(data.end_time);
$(".modal-details").text(data.details);
$(".modal-venue").text(data.venue);
$(".modal-cost").text(data.cost);
}, "json" );
}else{
$(this).toggleClass('selected');
}
});
$('#button').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
});
}); </script>
the line im having issue with is;
var event = "details.php?id=" + ($(this).data('id'));
I'm using ($(this).data('id')); to try and grab the id attribute from the button, but i keep getting undefined.
I've managed to figure out how to grab external json and use that in a modal using a manually set id in the javascript.
I'm stuck on passing the id from the button to the javascript url. My button markup is as follows;
<button type="button" class="btn btn-primary btn-sm modal-btn" data-id="10">Details</button>
My JS currently looks like this;
<script type="application/javascript">
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'T<"clear">lfrtip',
tableTools: {
"sRowSelect": "multi",
"aButtons": [ "select_all", "select_none" ],
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ 1 ] }
]
}
});
$('#example tbody').on( 'click', 'tr', function (e) {
if($(e.target).is('.modal-btn')){
$('#fullDetails').modal('show');
var event = "details.php?id=" + ($(this).data('id'));
$.getJSON(event, function( data ) {
$(".modal-title").text(data.title);
$(".modal-audience").text(data.audience);
$(".modal-leader").text(data.leader);
$(".modal-date").text(data.date);
$(".modal-start_time").text(data.start_time);
$(".modal-end_time").text(data.end_time);
$(".modal-details").text(data.details);
$(".modal-venue").text(data.venue);
$(".modal-cost").text(data.cost);
}, "json" );
}else{
$(this).toggleClass('selected');
}
});
$('#button').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
});
}); </script>
the line im having issue with is;
var event = "details.php?id=" + ($(this).data('id'));
I'm using ($(this).data('id')); to try and grab the id attribute from the button, but i keep getting undefined.
Share Improve this question asked Apr 22, 2015 at 13:16 NathanNathan 2,5134 gold badges37 silver badges49 bronze badges 1-
2
$(this)
is going to be referencing thetr
from the initial.on( 'click', 'tr', function (e)
, use.proxy()
, or just usevar event = "details.php?id=" + ($(e.target).data('id'));
– Josh Stevenson Commented Apr 22, 2015 at 13:21
3 Answers
Reset to default 4Try
var event = "details.php?id=" + ($(e.target).data('id'));
$(this) is pointing to the tr that was clicked.
Try to fetch it as below
$('#example tbody').on( 'click', 'tr', function (e) {
var target = $( e.target );//Try this change
if(target.is('.modal-btn')){ //pare it this way
$('#fullDetails').modal('show');
var event = "details.php?id=" + (target.data('id'));//fetch it like this
$.getJSON(event, function( data ) {
$(".modal-title").text(data.title);
$(".modal-audience").text(data.audience);
$(".modal-leader").text(data.leader);
$(".modal-date").text(data.date);
$(".modal-start_time").text(data.start_time);
$(".modal-end_time").text(data.end_time);
$(".modal-details").text(data.details);
$(".modal-venue").text(data.venue);
$(".modal-cost").text(data.cost);
}, "json" );
}else{
$(this).toggleClass('selected');
}
});
Change the line to
var event = "details.php?id=" + $(e.target).data('id');
and try again
本文标签: javascriptGetting an ID from a button into a bootstrap modalStack Overflow
版权声明:本文标题:javascript - Getting an ID from a button into a bootstrap modal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745527626a2661910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论