1 回答

TA貢獻1824條經(jīng)驗 獲得超6個贊
幾點:
您需要一個value,否則 value 為空。
你
yourServerSideFunc(body)
有兩個返回值,顯然第二個沒有被執(zhí)行。如果要將用戶重定向到不同的頁面,請通過重定向來實現(xiàn)。但是如果你想更新基本 html中的內容,那么你可以使用 HTML 元素的 style 屬性來隱藏或顯示元素。
您還可以在您的中實現(xiàn)一些條件
doGet(e)
,并根據(jù)參數(shù)或其集合呈現(xiàn)不同的內容。這將在同一 URL 處生成不同的 HTML。
例如:
1- 用戶訪問您的網(wǎng)絡應用程序,因此doGet()
被執(zhí)行。
2-用戶從下拉列表中選擇一個選項,因此瀏覽器偵聽點擊事件并執(zhí)行listS()
。
3-將返回 HTML 內容listS()
的用戶選擇的值傳遞給回調。yourServerSideFunc
showDiv
4-showDiv
執(zhí)行并更新 HTML 內容。
代碼.gs
function doGet() {
var output = HtmlService.createHtmlOutputFromFile('f');
return output;
}
function yourServerSideFunc(body) {
console.log(body);
var value = body["value"];
var output = HtmlService.createTemplateFromFile('f2');
output.value = value;
return output.evaluate().getContent();
}
f.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p style="text-align: center;">Test - Step 1</p>
<p style="text-align: center;"> </p>
<select id="sel1" style="width: 230px; height: 35px; margin: 0px 0 0px 0;">
<option selected="selected" value="email">email</option>
</select>
<div id="resultDiv" style="display: None;"></div>
<script>
function listS() {
const selectElem = document.getElementById('sel1');
const index = selectElem.selectedIndex;
if (index > -1) {
const e = document.getElementById("sel1");
const value = e.options[index].value;
const body = {
index: index,
value: value
};
google.script.run.withSuccessHandler(showDiv).yourServerSideFunc(body);
}
}
function showDiv(value) {
var resultDiv = document.getElementById("resultDiv");
resultDiv.innerHTML = value;
resultDiv.style.display = "Block";
}
document.getElementById("sel1").addEventListener("click", listS);
</script>
</body>
</html>
f2.html
<p id="test"style="text-align: center;">Your value is</p>
<p style="text-align: center;"><?= value ?></p>
進一步閱讀:
添加回答
舉報