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

javascript - How to pass a string to a bootstrap modal? - Stack Overflow

programmeradmin1浏览0评论

I'm not sure what I'm doing wrong, but I've searched in all "passing data to a bootstrap modal" posts, but still can't find the answer.

I have a table with users. In each row there is a "delete user button". When you click on "delete", a modal shows up which the legend: "Are you sure you want to delete?"

I want the username to be displayed in that legend, so I send the username to a JS file but it doesn't show. Nothing appears.

This is the delete button in every row:

   <td><a href="#" onclick="callToModal({$frontuser->username});return false;" class="btn mini red-stripe" role="button" >Delete</a></td>


I'm using smarty that's why the parameter looks like this: {$frontuser->username}

Above that code, I have the definition of the modal:

 <!-- modal -->
 <div id="myModal3" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
       <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
             <h3 id="myModalLabel3"></h3>
       </div>
       <div class="modal-body">
              <p></p>                              
       </div>
       <div class="modal-footer">
             <button class="btn" data-dismiss="modal" aria-hidden="true">Cerrar</button>
             <button data-dismiss="modal" class="btn blue" id="deleteok">Confirmar</button>
       </div>
</div>
<!-- end modal -->

Then my JS file is:

function callToModal(data) {
  $("#myModal3 .modal-header h3").html("Eliminar");
  $("#myModal3 .modal-body p").html("Are you sure you want to delete: ", data);
  $("#myModal3").modal("show");
}


Any help would be much appreciated!

I'm not sure what I'm doing wrong, but I've searched in all "passing data to a bootstrap modal" posts, but still can't find the answer.

I have a table with users. In each row there is a "delete user button". When you click on "delete", a modal shows up which the legend: "Are you sure you want to delete?"

I want the username to be displayed in that legend, so I send the username to a JS file but it doesn't show. Nothing appears.

This is the delete button in every row:

   <td><a href="#" onclick="callToModal({$frontuser->username});return false;" class="btn mini red-stripe" role="button" >Delete</a></td>


I'm using smarty that's why the parameter looks like this: {$frontuser->username}

Above that code, I have the definition of the modal:

 <!-- modal -->
 <div id="myModal3" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
       <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
             <h3 id="myModalLabel3"></h3>
       </div>
       <div class="modal-body">
              <p></p>                              
       </div>
       <div class="modal-footer">
             <button class="btn" data-dismiss="modal" aria-hidden="true">Cerrar</button>
             <button data-dismiss="modal" class="btn blue" id="deleteok">Confirmar</button>
       </div>
</div>
<!-- end modal -->

Then my JS file is:

function callToModal(data) {
  $("#myModal3 .modal-header h3").html("Eliminar");
  $("#myModal3 .modal-body p").html("Are you sure you want to delete: ", data);
  $("#myModal3").modal("show");
}


Any help would be much appreciated!

Share Improve this question edited Dec 14, 2014 at 13:32 Zanon 30.9k21 gold badges118 silver badges126 bronze badges asked Nov 18, 2013 at 20:24 LimonLimon 1,7937 gold badges33 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Finally after searching about smarty and js sintax, i found what the problem was.

Simply, it needed some ' ' wrapping the parameter.

So the right way of sending a parameter to JS from SMARTY is this:

<td><a href="#" onclick="callToModal('{$frontuser->username}');" class="btn mini red-stripe" role="button" >Eliminar</a></td>

Why do you have the ma in your .html() call? Simple typo maybe?

$("#myModal3 .modal-body p").html("Are you sure you want to delete: ", data);

It looks like "data" is the name ing from the function call in the delete row? Just change the ma to a + to add the name to the string if that's what you're trying to do.

$("#myModal3 .modal-body p").html("Are you sure you want to delete: "+ data);
发布评论

评论列表(0)

  1. 暂无评论