如何在Bootstrap模态体内运行jquery代码

时间:2016-09-05 14:40:28

标签: javascript jquery html twitter-bootstrap

我尝试在显示模态后运行jquery代码。我不知道为什么我的jquery代码没有加载。我的chrome控制台中没有显示错误。我希望有人能帮我解决问题。

使用Bootstrap:3.3.7和Jquery 1.12.4

最好的问候

这里是我的代码:

                                <!DOCTYPE html>
                            <html lang="en">
                            <head>
                              <title>Bootstrap Example</title>
                              <meta charset="utf-8">
                              <meta name="viewport" content="width=device-width, initial-scale=1">
                              <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                              <link href="/apps/signature-pad/assets/jquery.signaturepad.css" rel="stylesheet">
                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
                              <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
                              <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
                            </head>
                            <body>

                            <div class="container">
                              <h2>Modal Example</h2>
                              <!-- Trigger the modal with a button -->
                              <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

                              <!-- Modal -->
                              <div class="modal fade" id="myModal" role="dialog">
                                <div class="modal-dialog">

                                  <!-- Modal content-->
                                  <div class="modal-content">
                                    <div class="modal-header">
                                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                                      <h4 class="modal-title">Modal Header</h4>
                                    </div>
                                    <div class="modal-body">





                                        <div id="sig1" class="sigPad signed" style="margin-top:30px;">
                                            <div class="sigWrapper" style="height:80px;">
                                                <canvas class="pad" width="230" height="80" style="cursor:pointer;"></canvas>
                                            </div>
                                        </div>
                                        <script src="/apps/signature-pad/assets/numeric-1.2.6.min.js"></script>
                                          <script src="/apps/signature-pad/assets/bezier.js"></script>
                                          <script src="/apps/signature-pad/jquery.signaturepad.js"></script>
                                          <script>
                                          var sig = [{"lx":104,"ly":80,"mx":104,"my":79},{"lx":103,"ly":79,"mx":104,"my":80},{"lx":101,"ly":79,"mx":103,"my":79},{"lx":100,"ly":80,"mx":101,"my":79},{"lx":98,"ly":82,"mx":100,"my":80},{"lx":96,"ly":84,"mx":98,"my":82},{"lx":93,"ly":88,"mx":96,"my":84},{"lx":89,"ly":93,"mx":93,"my":88},{"lx":87,"ly":96,"mx":89,"my":93},{"lx":83,"ly":104,"mx":87,"my":96},{"lx":80,"ly":108,"mx":83,"my":104},{"lx":78,"ly":114,"mx":80,"my":108},{"lx":77,"ly":120,"mx":78,"my":114},{"lx":76,"ly":123,"mx":77,"my":120},{"lx":76,"ly":125,"mx":76,"my":123},{"lx":76,"ly":129,"mx":76,"my":125},{"lx":76,"ly":133,"mx":76,"my":129},{"lx":78,"ly":135,"mx":76,"my":133},{"lx":79,"ly":138,"mx":78,"my":135},{"lx":82,"ly":141,"mx":79,"my":138},{"lx":85,"ly":143,"mx":82,"my":141},{"lx":89,"ly":145,"mx":85,"my":143},{"lx":95,"ly":148,"mx":89,"my":145},{"lx":102,"ly":150,"mx":95,"my":148},{"lx":110,"ly":152,"mx":102,"my":150},{"lx":119,"ly":153,"mx":110,"my":152},{"lx":126,"ly":154,"mx":119,"my":153},{"lx":134,"ly":154,"mx":126,"my":154},{"lx":142,"ly":153,"mx":134,"my":154},{"lx":150,"ly":150,"mx":142,"my":153},{"lx":157,"ly":146,"mx":150,"my":150},{"lx":159,"ly":143,"mx":157,"my":146},{"lx":161,"ly":140,"mx":159,"my":143},{"lx":162,"ly":137,"mx":161,"my":140},{"lx":162,"ly":134,"mx":162,"my":137},{"lx":162,"ly":131,"mx":162,"my":134},{"lx":162,"ly":129,"mx":162,"my":131},{"lx":162,"ly":128,"mx":162,"my":129},{"lx":160,"ly":126,"mx":162,"my":128},{"lx":158,"ly":123,"mx":160,"my":126},{"lx":156,"ly":123,"mx":158,"my":123},{"lx":155,"ly":122,"mx":156,"my":123},{"lx":154,"ly":122,"mx":155,"my":122},{"lx":153,"ly":121,"mx":154,"my":122},{"lx":152,"ly":121,"mx":153,"my":121},{"lx":151,"ly":121,"mx":152,"my":121},{"lx":150,"ly":121,"mx":151,"my":121},{"lx":205,"ly":142,"mx":205,"my":141},{"lx":206,"ly":134,"mx":205,"my":142},{"lx":210,"ly":124,"mx":206,"my":134},{"lx":214,"ly":111,"mx":210,"my":124},{"lx":220,"ly":99,"mx":214,"my":111},{"lx":227,"ly":84,"mx":220,"my":99},{"lx":230,"ly":75,"mx":227,"my":84},{"lx":235,"ly":67,"mx":230,"my":75},{"lx":239,"ly":60,"mx":235,"my":67},{"lx":240,"ly":58,"mx":239,"my":60},{"lx":240,"ly":60,"mx":240,"my":58},{"lx":240,"ly":66,"mx":240,"my":60},{"lx":239,"ly":74,"mx":240,"my":66},{"lx":239,"ly":81,"mx":239,"my":74},{"lx":239,"ly":88,"mx":239,"my":81},{"lx":242,"ly":95,"mx":239,"my":88},{"lx":242,"ly":100,"mx":242,"my":95},{"lx":242,"ly":104,"mx":242,"my":100},{"lx":242,"ly":107,"mx":242,"my":104},{"lx":243,"ly":109,"mx":242,"my":107},{"lx":245,"ly":109,"mx":243,"my":109},{"lx":245,"ly":111,"mx":245,"my":109},{"lx":246,"ly":111,"mx":245,"my":111},{"lx":247,"ly":111,"mx":246,"my":111},{"lx":250,"ly":113,"mx":247,"my":111},{"lx":257,"ly":119,"mx":250,"my":113},{"lx":269,"ly":128,"mx":257,"my":119},{"lx":278,"ly":136,"mx":269,"my":128},{"lx":294,"ly":141,"mx":278,"my":136},{"lx":316,"ly":145,"mx":294,"my":141},{"lx":334,"ly":147,"mx":316,"my":145},{"lx":347,"ly":147,"mx":334,"my":147},{"lx":357,"ly":147,"mx":347,"my":147},{"lx":363,"ly":146,"mx":357,"my":147},{"lx":366,"ly":145,"mx":363,"my":146}];
                                          </script>
                                            <script>

                                            $(document).ready(function() {
                                              $('#sig1').signaturePad({displayOnly:true, autoscale:true, drawBezierCurves:true, variableStrokeWidth:true}).regenerate(sig);
                                            });
                                          </script>

                                            <script src="/apps/signature-pad/assets/json2.min.js"></script>





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

                                </div>
                              </div>

                            </div>

                            </body>
                            </html>

2 个答案:

答案 0 :(得分:3)

该代码正在文档的就绪事件上执行,而不是模式中执行。您必须将其绑定到modal event

$(document).ready(function() {
    $('#myModal').on('shown.bs.modal', function () {
        $('#sig1').signaturePad({displayOnly:true, autoscale:true, drawBezierCurves:true, variableStrokeWidth:true}).regenerate(sig);
    })
});

答案 1 :(得分:0)

您可以从脚本中的任何位置运行JavaScript,而不仅仅是在模式中运行JavaScript。 这将在模态打开时调用。

    $( "#myModal" ).on('shown.bs.modal', function(){
        alert("Modal Opened!");
    });

alert("Modal Opened!");替换为打开模态时要运行的代码