Okay, so I have the following HTML code:

<!DOCTYPE html>
<html>
  <head>
    <title>FastCast</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/footer.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
    <link rel="shortcut icon" href="favicon1.ico"/>
  </head>
  <body>
    <center>
    <h1><b>Welcome to FastCast!</b></h1>
    <div>
    <p><font size="3">FastCast is a web application designed to gather weather information from various sources and display it in an easy-to-read format.</font></p>
    <hr></hr>
    <div id="picDiv">
    <img id="imgDisp" src="logo.jpg">
    </div>
    <p><font size="6">Enter you location to get started.</font></p>
    </div> 
    <form class="form-inline" role="form">
    <div class="form-group">
    <input type="text" class="form-control" id="Location" placeholder="ex. Boston, MA">
    </div>
    </form>
    <p><i>Press enter to continue.</i></p>
    <a id="test">Click me</a>
    <script>
    window.onkeydown = function(event) {
        if (event.keyCode === 13) {
            var x = document.getElementById("Location").value;
            var last2 = x.slice(-2);
            alert(last2);
        }
    }
    var i = 0;
    $('a#test').click(function() {
        i += 1;

        $('a#test').popover({
            trigger: 'manual',
            placement: 'right',
            content: function() {
               var message = last2;
                 return message;
            }
        });
        $('a#test').popover("show");

    });
    </script>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    </center>
    </body>
    <div class="footer" id="footer">Developed in Twitter Bootstrap | Information from openweathermap.org | Tyler Jablonski, 2014+</div>
</html>

Towards the end I have a link that says "Click me". As you can see, in the script tags that follow, I have a bit of code that looks like this:

var i = 0;
    $('a#test').click(function() {
        i += 1;

        $('a#test').popover({
            trigger: 'manual',
            placement: 'right',
            content: function() {
               var message = last2;
                 return message;
            }
        });
        $('a#test').popover("show");

    });

I was hoping that this code would active a popover when the link is clicked, but for some reason, it doesn't work. Nothing happens at all. Why is this, and how can I properly make a popover in Bootstrap and then have it display a JavaScript variable?

Thanks!

有帮助吗?

解决方案

You are missing some things in your popover element <a> tag, have the format be something like this:

<a id="test" data-toggle="tooltip" rel="popover">Click Me</a>

Then it should work, for example: http://jsfiddle.net/52VtD/1983/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top