【问题标题】:How to pass a string to a bootstrap modal?如何将字符串传递给引导模式?
【发布时间】:2013-11-18 20:24:17
【问题描述】:

我不确定自己做错了什么,但我搜索了所有“将数据传递给引导模式”的帖子,但仍然找不到答案。

我有一张有用户的桌子。在每一行中都有一个“删除用户按钮”。当您单击“删除”时,会显示一个模式,其中的图例:“您确定要删除吗?”

我希望用户名显示在该图例中,因此我将用户名发送到 JS 文件但它没有显示。什么都没有出现。

这是每一行的删除按钮:

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


我正在使用 smarty,这就是为什么参数看起来像这样:{$frontuser-&gt;username}

在那段代码之上,我有模态的定义:

 <!-- 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 -->

那么我的JS文件是:

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");
}


任何帮助将不胜感激!

【问题讨论】:

    标签: javascript jquery twitter-bootstrap modal-dialog smarty


    【解决方案1】:

    终于在搜索了smarty和js sintax之后,我发现了问题所在。

    简单地说,它需要一些 ' ' 包装参数。

    所以从SMARTY向JS发送参数的正确方式是这样的:

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

    【讨论】:

    • @Daved yes :) 我只需要引号''
    【解决方案2】:

    为什么在 .html() 调用中有逗号?可能是简单的错字?

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

    看起来“数据”是来自删除行中的函数调用的名称?如果您要这样做,只需将逗号更改为 + 即可将名称添加到字符串中。

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

    【讨论】:

    • 非常感谢您的回答。我将逗号更改为+,现在它可以工作,但只能用于数字,而不是我想要的字符串。我正在做一些测试,我没有发送用户名作为参数,而是发送了他的 ID,它完美地出现在模态框上。但这不是字符串的情况,我不知道为什么。是的,数据是我从这句话中发送的参数:删除
    • 当我在浏览器上检查时,出现 1 个错误,它表示未定义用户。我真的不明白为什么。我认为问题可能是因为函数调用在 foreach 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2015-01-27
    • 2014-01-05
    • 2013-07-21
    • 2020-06-13
    • 2016-01-01
    相关资源
    最近更新 更多