简体   繁体   English

引导模态的Javascript变量

[英]Javascript Variable to Bootstrap Modal

Got what is probably a simple one, but my google is failing me at the moment so hoping i can get a bit of a push in the right direction 有可能是一个简单的方法,但是我的谷歌目前使我失望了,希望我能在正确的方向上有所作为

I have a HTML page that is basically a bunch of checkboxes. 我有一个HTML页面,基本上是一堆复选框。 These checkboxes are for simple self evaluation yes/no questionnaire that are weighted with different values based on the questions. 这些复选框用于简单的自我评估“是/否”问卷,并根据问题以不同的值加权。

At the end of the questionnaire, you hit calculate results and it used a quick Javascript function to calculate the score and an Alert Popup give you the score 在调查表的末尾,您点击了计算结果,它使用了快速的Javascript函数来计算分数,并且Alert Popup会为您提供分数

I recently migrated the page to Bootstrap 3 and cleaned it up a bit but want to use a Modal to popup instead of the old style Javascript alert 我最近将页面迁移到Bootstrap 3并进行了一些清理,但想使用Modal弹出而不是旧式的Javascript警报

Is there a simple way of passing the javascript calculated variable from the main page to a Bootstrap Modal 有没有一种简单的方法可以将javascript计算的变量从主页传递到Bootstrap Modal

Thanks 谢谢

EDIT 编辑

Right - Worked it out, was calling the JQUERY code 正确-解决了问题,正在调用JQUERY代码

$('#myResults').html(counter);

From within my Model - moved the snippet into the main javascript function and it works. 从我的模型中-将代码段移至主要的javascript函数中,并且可以正常工作。 Knew it was going to be a simple one.. Might be time to stop working i think 知道这将是一个简单的..我想可能是时候停止工作了

Thanks 谢谢

Yes . 是的。 You can pass variable to bootstrap modal. 您可以将变量传递给引导模态。

Script file 脚本文件

     <script>
 function testFun(variable){

document.getElementById('testH1').innerHTML=variable;
}
</script>

HTML HTML

<button class="btn btn-primary btn-lg" onclick="testFun('testId')" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>
<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-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <h1 id="testH1"></h1>
      </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>

Take a look at the documentation . 看一下文档 I believe what you are going to want to do is add a data-toggle="modal" attribute to your calculate button and then make it so when it calls your calculate function, it changes the content in the modal via Javascript. 我相信您想要做的就是在您的按钮中添加data-toggle="modal"属性,然后使其生效,以便当它调用您的calculate函数时,它将通过Javascript更改模式中的内容。

The modal is just normal HTML and you can assign ids to whatever you want. 模态只是普通的HTML,您可以将id分配给您想要的任何东西。 From there you can edit it with innerHTML 在这里,您可以使用innerHTML对其进行编辑

<!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn" data-toggle="modal">Calculate</a>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <h3 id="myModalLabel"></h3>
  </div>
</div>

Javascript 使用Javascript

function calculate() {
    //do some calculation and store it in a variable
    var a = b + c; 
    document.getElementById("myModalLabel").innerHTML = "The final calculation is" + a;
}

Hope I understood your question. 希望我理解你的问题。

Bootstrap modal doesn't support this functionality. Bootstrap模态不支持此功能。 But you could write your own? 但是你可以自己写?

function showModal(score){
  $('#myModal .score').html(score);
  $('#myModal').modal('show')
}

so clicking the calculate results what look something like this. 所以单击计算结果看起来像这样。

$('#buttonCalcResults').click(function(){
  var score = foo+bar+whatever
  showModal(score);
})

As Jahnux73 already said (or meant) that the modal will be in the same page, where you want it to appear. 正如Jahnux73所说(或意味着)的那样,该模式将在您希望它出现的同一页面中。 So in the function where you are calling an alert box you just have to call that modal. 因此,在调用警报框的函数中,您只需要调用该模式即可。

 $('#yourModalId').modal('toggle');

(or you can also use 'show' instead of toggle) And for the calculated values...you just need to add these values to the html of modal. (或者您也可以使用“显示”而不是切换),对于计算出的值……您只需将这些值添加到模态html中即可。 So just get the element. 因此,只需获取元素。

output = document.getElementById('yourOutputTagId');

and then you can set the properties of that output object; 然后可以设置该输出对象的属性; either you want innerHTML or value attribute (according to your design.) 您需要innerHTML还是value属性(根据您的设计)。

After setting all the required value you call your modal by the function mentioned above. 设置所有必需值后,您可以通过上述功能调用模态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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