简体   繁体   English

如何使用 select 中的参数调用 onchange 事件

[英]How to call onchange event with parameter from select

I have this code.我有这个代码。 One dropdown name="street" is populated, then another dropdown is populated according to the value of the prior dropdown.填充一个下拉列表name="street" ,然后根据前一个下拉列表的值填充另一个下拉列表。

Now I have to add an radio button and populate the second dropdown according to option checked and first dropdown.现在我必须添加一个单选按钮并根据选中的选项和第一个下拉列表填充第二个下拉列表。

I planed to add onchange event in one radio button, but I don't know how to pass as parameter the value for the first dropdown.我计划在一个单选按钮中添加onchange事件,但我不知道如何将第一个下拉列表的值作为参数传递。

This is not working:这不起作用:

onchange="GetNumber(<script>document.getElementById("street").value<script>)"

segment部分

   <script>
        function GetNumber(street) 
        {
        
        google.script.run.withSuccessHandler(function(ar) 
        {
    
        console.log(ar);
        
        number.length = 0;
        
        let option = document.createElement("option");
        option.value = "";
        option.text = "";
        number.appendChild(option);
        
        ar.forEach(function(item, index) 
        {    
          let option = document.createElement("option");
          option.value = item[2];
          option.text = item[1];
          number.appendChild(option);    
        });
        
        }).getNumbers(street);
        


  

segment部分

<form method="post" id="form" action="<?= url ?>" >   
        
          <label >Indirizzo</label><br>
          <select name="street" onchange="GetNumber(this.value)" >
          <option value="" ></option>
          <? for(var i = 0; i < streets.length; i++) { ?>      
          <option value="<?= streets[i] ?>" ><?= streets[i] ?></option>
          <? } ?>
          </select><br><br>
          <label class="radio-inline">
              <input type="radio" name="addressType" value="Domestic"   ****magic needed here****>Domestic</label>
              
           <label class="radio-inline">
              <input type="radio" name="addressType" value="Non-Domestic" checked> Non-Domestic</label><br>
          <label >Civico</label><br>
          <select name="number" id="number" onchange="ShowCondominiums(this.value)" >
          </select><br><br>
          <span id="Condominiums" >Condominiums: </span><br>
          <label >Some Data to Store as Sample</label>>
          <input type="text" name="name" /><br>

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve both the value of 1st dropdown list and the value of radio button.您想要检索第一个下拉列表的值和单选按钮的值。
  • You want to update the values of the 2nd dropdown list by using the retrieved values.您想使用检索到的值更新第二个下拉列表的值。

Modification points:修改点:

  • When I saw your HTML, I confirmed <input type="radio" name="addressType" value="Non-Domestic" checked> .当我看到您的 HTML 时,我确认了<input type="radio" name="addressType" value="Non-Domestic" checked> In this modification, I would like to propose to use this value as the default value.在这个修改中,我想建议使用这个值作为默认值。
  • In this modification, when the 1st dropdown list and the radio button are changed, the values of 2nd dropdown list are updated by GetNumber() .在此修改中,当更改第一个下拉列表和单选按钮时,第二个下拉列表的值由GetNumber()更新。

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

HTML&Javascript side: index.html HTML&Javascript 端: index.html

<form method="post" id="form">
<label >Indirizzo</label><br>
<select name="street" id="select1" onchange="GetNumber('Non-Domestic')" >
<option value="" ></option>
<? for(var i = 0; i < streets.length; i++) { ?>
<option value="<?= streets[i] ?>" ><?= streets[i] ?></option>
<? } ?>
</select><br><br>
<label class="radio-inline">
<input type="radio" name="addressType" value="Domestic" onchange="GetNumber(this.value)">Domestic</label>
<label class="radio-inline">
<input type="radio" name="addressType" value="Non-Domestic" onchange="GetNumber(this.value)" checked> Non-Domestic</label><br>
<label >Civico</label><br>
<select name="number" id="number" onchange="ShowCondominiums(this.value)" >
</select><br><br>
<span id="Condominiums" >Condominiums: </span><br>
<label >Some Data to Store as Sample</label>>
<input type="text" name="name" /><br>
</form>

<script>
function GetNumber(addressType){
  var street = document.getElementById("select1").value;
  google.script.run.withSuccessHandler(function(ar){
    console.log(ar);
    number.length = 0;
    let option = document.createElement("option");
    option.value = "";
    option.text = "";
    number.appendChild(option);
    ar.forEach(function(item, index){
      let option = document.createElement("option");
      option.value = item[2];
      option.text = item[1];
      number.appendChild(option);
    });
  }).getNumbers(street, addressType);
}
</script>

Google Apps Script side: Google Apps 脚本方面:

Unfortunately, I cannot find your Google Apps Script side.很遗憾,我找不到您的 Google Apps 脚本端。 So in this modification, I propose the sample script for testing above HTML&Javascript.所以在这个修改中,我提出了用于测试以上 HTML&Javascript 的示例脚本。

function openDialog() {
  var html = HtmlService.createTemplateFromFile('index');
  html.streets = ["a", "b", "c"];
  SpreadsheetApp.getUi().showModalDialog(html.evaluate(), "sample");
}

function getNumbers(street, addressType) {
  return [["", street, street], ["", addressType, addressType]];
}
  • When you use this modified script, please run openDialog at the script editor.当您使用此修改后的脚本时,请在脚本编辑器中运行openDialog By this, a dialog is opened on Spreadsheet and you can test the modified script.这样,将在电子表格上打开一个对话框,您可以测试修改后的脚本。 So, please copy and paste the above scripts to the container-bound script of Spreadsheet.因此,请将上述脚本复制并粘贴到电子表格的容器绑定脚本中。
  • When you open a dialog, and you change the 1st dropdown list is changed, the selected value of 1st dropdown list and the default value ( Non-Domestic ) of the radio button are sent to getNumbers(street, addressType) .当您打开对话框并更改第一个下拉列表时,第一个下拉列表的选定值和单选按钮的默认值( Non-Domestic )将发送到getNumbers(street, addressType) By this, the 2nd dropdown list is updated.这样,第二个下拉列表被更新。 When you change the radio button, the selected value of 1st dropdown list and the changed value of radio button are sent to getNumbers(street, addressType) .当您更改单选按钮时,第一个下拉列表的选定值和单选按钮的更改值将发送到getNumbers(street, addressType) By this, the 2nd dropdown list is updated again.这样,第二个下拉列表再次更新。

Note:笔记:

  • In this modification, I'm not sure about the script of your Google Apps Script side.在此修改中,我不确定您的 Google Apps 脚本端的脚本。 So please modify your actual script using above modified script.因此,请使用上述修改后的脚本修改您的实际脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM