简体   繁体   中英

HTML drop-down box with Google App Engine

I am creating a Google App Engine web application written in Python, and I would like to create a drop down box that displays different values corresponding to pages of a book that a user could choose from. I would like the action of the drop down box to be to direct the user to the page that corresponds to this link:

<a href='/viewpage/{{bookpage.key}}'>{{ bookpage.page }} </a>

The "bookpage" entity is passed to the html

Thank you!

David

Use a Jump Menu . Here is a pretty straight forward implementation.

Basically you'll just add a bit of JavaScript, and instead of writing an a tag, you'll write an option:

<option value='/viewpage/{{bookpage.key}}'>{{ bookpage.page }} </option>

What about <option value='/viewpage/{{bookpage.key.id}}'>{{bookpage.page}}</option> ?

I hope it's not a dumb answer.

I'm not familiar with the google-app-engine but, the following javascript seems to do what you want. The python could generate the array variables on the server side, and then everything else would work properly. I included the hardcoded arrays so you can see what is going on, but you can replace the arrays with the python code(assuming bookpage is some kind of dictionary):

i = 0
for bp in bookpage.keys():
    print("mysites["+str(i)+"] = "+ bookpage[bp])+";"
    print("sitenames["+str(i)+"] = "+sitenames[bp])+";"
    i+=1

<html>
<body>
<script type="text/javascript">
var mysites= new Array();
    mysites[0] = "http://www.google.com";   //Generate this line with python
    mysites[1] = "http://www.bing.com";     //Generate this line with python
    mysites[2] = "http://www.yahoo.com"; //Generate this line with python
var sitenames = new Array();
    sitenames[0] = "Google";       //Generate this line with python
    sitenames[1] = "Bing";           //Generate this line with python
    sitenames[2] = "Yahoo";       //Generate this line with python
function changeLink(){
    var index = document.getElementById("theselect").selectedIndex
document.getElementById("thelink").innerHTML=index;
    var newlink = mysites[index];
    var newstring = sitenames[index];
    document.getElementById("thelink").href=newlink;
    document.getElementById("thelink").innerHTML=sitenames[index];
}
</script>
<select id="theselect" onclick="changeLink()">
  <option>Google</option>
  <option>Bing</option>
  <option>Yahoo</option>
</select>
 <br />
<a id="thelink" href="http://www.google.com" > Google </a>
</body>
</html>

Clicking on the option box calls the changeLink() function, which then changes the link and the inner html of the tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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