代码之家  ›  专栏  ›  技术社区  ›  Poorna Senani Gamage

如何在单击按钮时显示模态

  •  1
  • Poorna Senani Gamage  · 技术社区  · 6 年前

    我需要打开一个模式使用按钮的 onclick ,但没有成功。我的代码有错误吗?

    <button class="btn btn-default" id="addtobag" onclick="addTobag();">Add to Bag</button><br>
    

    情态动词

    <div class="modal fade" id="addtobagmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    //mycode
                </div>
                <div class="modal-body">
                    //mycode
                </div>
                <div class="modal-footer">
                    //mycode
                </div>
            </div>
        </div>
    </div>
    

    功能

    function addTobag() {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                if (request.responseText == "OK") {
                    $('#addtobagmodal').modal('show');
                } else {
                    alert('error');
                }
            }
        };
        request.open("GET", "myBag", true);
        request.send();
    }
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   saAction    6 年前

    问题在于 "myBag" 以及它的内容。

    有两个条件你正在检查

    if (request.readyState === 4 && request.status === 200)
    

    第二个呢

    if (request.responseText == "OK")
    

    这意味着模型只有在两个条件都正确时才会显示。

    创建文本文件 myBag.txt 应用OK(仅该文件中的OK)

    function addTobag() {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                if (request.responseText == "OK") {
                    $('#addtobagmodal').modal('show');
                } else {
                    alert('error');
                }
            }
            else{
                alert('myBag file not found,  request.readyState = ' + request.readyState);
            }
    
        };
        request.open("GET", "myBag.txt", true);
        request.send();
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <div class="modal fade" id="addtobagmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    //mycode
                </div>
                <div class="modal-body">
                    //mycode
                </div>
                <div class="modal-footer">
                    //mycode
                </div>
            </div>
        </div>
    </div>
    
    <button class="btn btn-default" id="addtobag" onclick="addTobag();">Add to Bag</button><br>

    在这里确保“myBag.txt文件“”在HTML文件的同一路径上。

    注释文件将是:

        2
  •  1
  •   Avijit Barua    6 年前

    您可以尝试编写按钮代码,如

    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" onclick="addTobag();>Add to Bag </button>
    

    使用模态函数

       <div class="modal fade" id="myModal" role="dialog">
                            <div class="modal-dialog">
    <div class="modal-content">
                <div class="modal-header">
                    //mycode
                </div>
                <div class="modal-body">
                    //mycode
                </div>
                <div class="modal-footer">
                    //mycode
                </div>
            </div>
        </div>
    </div>
    
        3
  •  1
  •   Shiv Kumar Baghel    6 年前

    试试下面的代码。刚加的 js css 用于测试的libs。查看代码中缺少的内容。测试点击 测试

      function addTobag() {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
          if (request.readyState === 4 && request.status === 200) {
            if (request.responseText = "OK") {
              $('#addtobagmodal').modal('show');
            } else {
              alert('error');
            }
          }
        };
        request.open("GET", "myBag", true);
        request.send();
      }
    
      function test() {
        $('#addtobagmodal').modal('show');
      }
    <html>
    
      <head>
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
      </head>
      <body>
        <button class="btn btn-default" id="addtobag" onclick="addTobag();">Add to Bag</button><br>
        <button class="btn btn-default" id="addtobag" onclick="test();">Test modal only</button><br>
    
      <div class="modal fade" id="addtobagmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
          <div class="modal-content">
            <div class="modal-header">
              //mycode
            </div>
            <div class="modal-body">
              //mycode
            </div>
            <div class="modal-footer">
              //mycode
            </div>
          </div>
        </div>
      </div>
    
    </body>
    </html>