带有Google App Engine的HTML下拉框

1 投票
3 回答
767 浏览
提问于 2025-04-16 06:56

我正在创建一个用Python写的Google App Engine网页应用,我想做一个下拉框,里面显示用户可以选择的书页的不同值。下拉框的作用是让用户可以直接跳转到与这个链接对应的书页:

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

这个“bookpage”实体会传递到html中

谢谢!

大卫

3 个回答

0

那这个代码片段 <option value='/viewpage/{{bookpage.key.id}}'>{{bookpage.page}}</option> 呢?

我希望这不是个傻问题。

0

我对谷歌应用引擎不太熟悉,不过下面的这段JavaScript代码似乎能满足你的需求。Python可以在服务器端生成数组变量,然后其他部分就能正常工作了。
我加了一些硬编码的数组,这样你可以看到发生了什么,但你可以用Python代码替换这些数组(假设bookpage是某种字典):

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>

点击选项框会调用changeLink()这个函数,然后它会改变链接和标签内的HTML内容。

1

使用一个跳转菜单这里有一个比较简单的实现方法。

基本上,你只需要添加一点JavaScript代码,代替写一个链接标签(a标签),你会写一个选项(option):

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

撰写回答