我設法使用來自
它有效,我得到了預期的回應。但似乎觸發功能只能使用一次。我的意思是,當我嘗試將 GetIslands(this.value) 用于第二行條目時,它不起作用。這是我嘗試反復使用 GetIslands(this.value) 時的情況:

當我為 B 選擇一個州時,A 的島嶼下拉串列發生了變化。
所以我不得不多次重復相同的代碼塊(因此 GetIslands2(this.value) 用于第二個條目)。必須有一種較少冗余的方法來做到這一點。任何人都可以幫忙嗎?提前致謝!
HTML 和 JAVASCRIPT
<label>State</label>
<select id="state1" onchange="GetIslands(this.value)" required></div>
<option value="" hidden>Select a state</option>
<? for(var i = 0; i < states.length; i ) { ?>
<option value="<?= states[i] ?>" ><?= states[i] ?></option>
<? } ?></select>
<label>Island</label>
<select name="island" id="island1" required>
<option value="" hidden>Select a marine park</option>
</select>
<label>State</label>
<select id="state2" onchange="GetIslands2(this.value)" required></div>
<option value="" hidden>Select a state</option>
<? for(var i = 0; i < states.length; i ) { ?>
<option value="<?= states[i] ?>" ><?= states[i] ?></option>
<? } ?></select>
<label>Island</label>
<select name="island2" id="island2" required>
<option value="" hidden>Select a marine park</option>
</select>
<script>
function GetIslands(state)
{
google.script.run.withSuccessHandler(function(ar)
{
console.log(ar);
island1.length = 0;
let option = document.createElement("option");
option.value = "";
option.text = "Select an island";
option.hidden = true;
island1.appendChild(option);
ar.forEach(function(item, index)
{
let option = document.createElement("option");
option.value = item;
option.text = item;
island1.appendChild(option);
});
}).getIslands(state);
};
function GetIslands2(state)
{
google.script.run.withSuccessHandler(function(ar)
{
console.log(ar);
island2.length = 0;
let option = document.createElement("option");
option.value = "";
option.text = "Select an island";
option.hidden = true;
island2.appendChild(option);
ar.forEach(function(item, index)
{
let option = document.createElement("option");
option.value = item;
option.text = item;
island2.appendChild(option);
});
}).getIslands(state);
};
</script>
應用程式腳本
function getState() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var siteSheet = ss.getSheetByName("Sites");
var getLastRow = siteSheet.getLastRow();
var return_array = [];
for(var i = 2; i <= getLastRow; i )
{
if(return_array.indexOf(siteSheet.getRange(i, 1).getValue()) === -1) {
return_array.push(siteSheet.getRange(i, 1).getValue());
}
}
return return_array;
}
function getIslands(state) {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var siteSheet = ss.getSheetByName("Sites");
var getLastRow = siteSheet.getLastRow();
var return_array = [];
for(var i = 2; i <= getLastRow; i )
{
if(siteSheet.getRange(i, 1).getValue() === state){
return_array.push(siteSheet.getRange(i, 2).getValue());
}
}
const unique = (value,index,self) => {return self.indexOf(value) ===index;}
var Unique_List=return_array.filter(unique);
return Unique_List;
}
uj5u.com熱心網友回復:
我相信你的目標如下。
- 更改時
state1,將值設定為island1。并且,當state2改變時,一個值被設定為island2。在當前腳本中,為了實作這一點,需要使用GetIslands和2個函式GetIslands2。 - 在您的目標中,您希望僅使用函式來實作這種情況
GetIslands。
在這種情況下,如何進行以下修改?
修改后的腳本:
在此修改中,您的顯示 HTML 修改如下。
<label>State</label>
<select id="state1" onchange="GetIslands(this)" required></div>
<option value="" hidden>Select a state</option>
<? for(var i = 0; i < states.length; i ) { ?>
<option value="<?= states[i] ?>" ><?= states[i] ?></option>
<? } ?></select>
<label>Island</label>
<select name="island" id="island1" required>
<option value="" hidden>Select a marine park</option>
</select>
<label>State</label>
<select id="state2" onchange="GetIslands(this)" required></div>
<option value="" hidden>Select a state</option>
<? for(var i = 0; i < states.length; i ) { ?>
<option value="<?= states[i] ?>" ><?= states[i] ?></option>
<? } ?></select>
<label>Island</label>
<select name="island2" id="island2" required>
<option value="" hidden>Select a marine park</option>
</select>
<script>
function GetIslands(e) {
const obj = {state1: island1, state2: island2};
google.script.run.withSuccessHandler(function (ar) {
console.log(ar);
obj[e.id].length = 0;
let option = document.createElement("option");
option.value = "";
option.text = "Select an island";
option.hidden = true;
obj[e.id].appendChild(option);
ar.forEach(function (item, index) {
let option = document.createElement("option");
option.value = item;
option.text = item;
obj[e.id].appendChild(option);
});
}).getIslands(e.value);
}
</script>
- 在該修改中,用于轉換已編輯標簽的物件被準備好,如
const obj = {state1: island1, state2: island2}。并且,使用它來管理每個標簽。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487917.html
