简体   繁体   中英

Getting on click event using boostrap modal

This is the js code :

$( document ).ready(function() {
        $('#myModal').on('shown.bs.modal', function () {
            console.log(this);
            p_info = $(this).attr('id').split("||");
            // $.ajax({
            //   method: "POST",
            //   url: "/pacients/add_new_disease",
            //   data: { pacient_id: p_info[0]}
            // })
            // .done(function( msg ) {
            //    $("#myModalLabel").text("Add disease - "+ p_info[1]);
            // });
        });
    });

This is the html part:

    <img src="http://localhost/test/images/pacients/update_pacient.jpg" id="25||Mihai Eminescu" class="update_pacient" data-toggle="modal" data-target="#myModal" alt="update">
    <div class="modal-dialog">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title" id="myModalLabel"></h4>
        </div>
        <div class="modal-body">
          <h4>Select diagnostic</h4>
          <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>
          <h4>Symptoms</h4>
          <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>
          <h4>Treatment</h4>
          <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>

        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>

      </div>
    </div>
  </div>

What I want to achieve is to get the id value from the img tag, with this , but obviously it is not working. Can you give me some idea how to get the id value when i click on the img and a modal is shown?

I am guessing $(this) refers to the modal rather than the image.

So try like this instead:

 p_info = $("img.update_pacient").attr('id').split("||");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM