我看到許多關于“要求至少選擇一個復選框”的問題,但對于使用 Google Apps 腳本開發的網路應用程式卻沒有。
在 Google Apps Script 網路應用程式中,我可以輕松地要求一個單選按鈕進行檢查:
<form id="requestNewKeys" onsubmit="handleFormSubmit(this)">
<p>
<label for="primaryToolRadioGroup">What is your primary purpose for requesting Tool XXXXXX</label>
</p>
<div role="radiogroup" id="primaryPurposeRadioGroup">
<input type="radio" id="primaryPurpose0" name="primaryPurpose" value="Research" required="">
<label for="primaryPurpose0">Research</label><br>
<input type="radio" id="primaryPurpose1" name="primaryPurpose" value="Teaching" required="">
<label for="primaryPurpose1">Teaching</label><br>
</div>
<input type="submit" value="Request License">
</form>
和 handleFormSubmit 函式:
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(updateResults).processForm(formObject);
}
但我無法弄清楚需要input type="checkbox"檢查一個或多個 s 的等價物。我知道 Google Apps Script 與 Javascript 非常相似,但在這種情況下,它似乎足以使我在這里找到的其他解決方案不起作用。
uj5u.com熱心網友回復:
您可以嘗試這種方法,如果選中的復選框數量至少為 1,它只會繼續進行應用程式腳本處理。
<!DOCTYPE html>
<html>
<body>
<form id="requestNewKeys">
<p>
<label for="primaryToolCheckboxGroup">What is your primary purpose for requesting Tool XXXXXX</label>
</p>
<div role="checkboxgroup" id="primaryPurposeCheckboxGroup">
<input type="checkbox" id="primaryPurpose0" name="primaryPurpose" value="Research">
<label for="primaryPurpose0">Research</label><br>
<input type="checkbox" id="primaryPurpose1" name="primaryPurpose" value="Teaching">
<label for="primaryPurpose1">Teaching</label><br>
</div>
<input type="button" value="Request License" onclick="checkForm()">
</form>
</body>
<script>
function checkForm() {
var checkboxes = document.getElementsByName('primaryPurpose');
var form = document.getElementById('requestNewKeys');
var checked = 0;
checkboxes.forEach(checkbox => {
if(checkbox.checked)
checked ;
});
if(checked > 0)
// google.script.run.withSuccessHandler(updateResults).processForm(form);
else
alert('Please check at least one checkbox!');
}
</script>
</html>
單擊請求許可證后:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494462.html
標籤:谷歌应用脚本
