javascript - 将变量传递给 Bootstrap 模态?

标签 javascript jquery asp.net twitter-bootstrap

我已经搜索了其他“变量到模态”线程,但我找不到我需要的答案。我有一个 ASP.NET 应用程序(非 MVC),我有一个生成一系列按钮的网格。我试图将模态操作保留在客户端。我生成的按钮代码如下所示:

    <input type="button" value="Add Note" 
    onclick="return false;javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$grd888Poker&#39;,&#39;Button$4001&#39;)" 
class="btn btn-primary btn-xs" 
data-toggle="modal" 
data-target="#myModal" 
entryID="4001" />

模式弹出就好了。但是我需要将属性 entryID 传递给模态中的隐藏字段。 Obv 我需要某种 JS,但由于我让 Bootstrap 处理模态代码,所以我没有什么可以覆盖的。在这种情况下如何将变量传递给模态?

最佳答案

您始终可以在模式显示时将事件附加到模态,并附加隐藏的输入字段或设置已存在的隐藏输入字段的值。每次显示模态时都应调用 show.bs.modal 事件,即使 Bootstrap 处理模态显示或者如果您从 JavaScript 显示它也是如此。

$('#myModal').on('show.bs.modal', function() {
    $("#entryField").val($('#myModal').attr("entryID"));
})

$('#myModal').on('show.bs.modal', function() {
    var hiddenField = $(document.createElement("input")).prop("type", "hidden").appendTo("#myModal"); 
})

按钮事件:

$("input[type='button']").click(function() {
    $("#myModal").find("input[type='hidden']").val($(this).attr("entryID"); 
});

请注意,这要求隐藏字段已经存在于模态窗口中。

关于javascript - 将变量传递给 Bootstrap 模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20642900/

相关文章:

javascript - dhtmlxscheduler 与 ASP.NET MVC5 的使用

javascript - JS : Splitting a long string into strings with char limit while avoiding splitting words

javascript - 使用 Codekit 时未定义“Angular ”

javascript - 将 cookie 中的值存储到 JavaScript 变量

dom - jQuery .each() 返回 DOM 元素而不是 jQuery 对象

asp.net - 如何在后面的代码中访问 span id

javascript - MouseEnter 事件未触发

javascript - 如何访问 reducer (redux-router)中的当前位置?

jquery - json 具有自动完成功能?

c# - 客户端 : if textbox1 and textbox 2 set counter++