最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get data values of a bootstrap modal using jQuery or javascript on clicking button inside the modal? - Stack Overflow

programmeradmin0浏览0评论

I want to get data values of a bootstrap modal on clicking button inside the modal. Here is my modal -

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="myModalLabel">Cancel</h4>
                                </div>
                                <div class="modal-body"> 
                                    Are you sure you want to cancel this?
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="savebutton" class="btn btn-success" >Yes</button>  
                                    <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                                </div>
                            </div>
                        </div> 
                    </div>

And here is the way I am passing the data to modal -

Update -

    <button data-target='#myModal' id='link' data-toggle='modal' data-id="1"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 1</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="2"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 2</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="3"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 3</button>
    ...
    ..
    .
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="n"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel n</button>

Modal can be called from any one of the many buttons and I need to get the data-id of the concerned button only. e.g.- if I click on "Cancel 1" button, I should get data-id as 1 after clicking "Yes" button in modal.

I want to get the modal data value of "id" field which is 1234 on clicking "Yes" button in this modal using jQuery or javascript.

I want to get data values of a bootstrap modal on clicking button inside the modal. Here is my modal -

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="myModalLabel">Cancel</h4>
                                </div>
                                <div class="modal-body"> 
                                    Are you sure you want to cancel this?
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="savebutton" class="btn btn-success" >Yes</button>  
                                    <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                                </div>
                            </div>
                        </div> 
                    </div>

And here is the way I am passing the data to modal -

Update -

    <button data-target='#myModal' id='link' data-toggle='modal' data-id="1"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 1</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="2"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 2</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="3"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 3</button>
    ...
    ..
    .
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="n"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel n</button>

Modal can be called from any one of the many buttons and I need to get the data-id of the concerned button only. e.g.- if I click on "Cancel 1" button, I should get data-id as 1 after clicking "Yes" button in modal.

I want to get the modal data value of "id" field which is 1234 on clicking "Yes" button in this modal using jQuery or javascript.

Share Improve this question edited Oct 18, 2016 at 13:57 gauravparmar asked Oct 18, 2016 at 11:39 gauravparmargauravparmar 9241 gold badge10 silver badges23 bronze badges 2
  • 1 var id=document.getElementById('savebutton').getAttribute("data-id"); – user1844933 Commented Oct 18, 2016 at 11:45
  • Thanks for help. I tried but it is returning null instead of the value. Here is the jsfiddle- jsfiddle.net/Au9tc/4771 – gauravparmar Commented Oct 18, 2016 at 12:31
Add a comment  | 

3 Answers 3

Reset to default 9

I have modifed your fiddle : http://jsfiddle.net/exr00e0d/

you had taken href. instead of that i took the target(modal).

$('.cancel_modal').click(function() {
   //alert('called');
    // we want to copy the 'id' from the button to the modal
    var href = $(this).data('target');
    var id = $(this).data('id');

    // since the value of href is what we want, so I just did it like so
    alert(href);
    // used it as the selector for the modal
    alert(id);
    $(href).data('id', id);
});

try this

function getid() {
var id=document.getElementById('savebutton').getAttribute("data-‌​id");
return id;
}

<button type="button" id="savebutton" class="btn btn-success" onclick="getid()" >Yes</button> 

In jQuery

$('#savebutton').click(function() {
    var id = $('#link').data('id');
    // if for some reason, the code above doesn't work,
    // $('#link').attr('data-id');
    console.log(id);
});

Edit:

Looking at your jsfiddle

$('#link').click(function() {
    // we want to copy the 'id' from the button to the modal
    var target = $(this).attr('target');
    var id = $(this).data('id');

    $(target).data('id', id);
});

$('#savebutton').click(function() {
    // now we grab it from the modal
    var id = $('#addBookDialog').data('id');

    console.log(id);
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论