2010-11-11 65 views
1

我正在創建一個用Python編寫的Google App Engine網絡應用程序,並且我想創建一個下拉框,顯示與用戶可以選擇的圖書頁面相對應的不同值從。我想下拉框的作用是將用戶定向到對應於該鏈接的頁面:使用Google App Engine的HTML下拉框

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

的「bookpage」實體傳遞給HTML

謝謝!

大衛

回答

1

使用Jump MenuHere是一個非常直接的實現。

基本上你只需要添加一個JavaScript代碼,而不是寫一個標籤,你會寫一個選項:

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

謝謝!這似乎很好! – David 2010-11-12 22:26:43

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。