【问题标题】:Get selected value from dynamic dropdown (options generated by javascript to ID)从动态下拉列表中获取选定的值(由 javascript 生成的选项到 ID)
【发布时间】:2019-01-18 08:06:29
【问题描述】:

我正在尝试获取用户选择的动态下拉列表的值(不是预先确定的)。我很确定我不能使用(也试过)document.getElementById("selectSubject").value;,因为它有冲突的 ID。我不能使用document.getElementByClass,因为我将使用它来通过 CSS 添加样式。

<script> //to generate the dynamic dropdown that is working
var select = document.getElementById("selectSubject");
if (arrayEmail[i] == email){
var opt = arraySubject[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
<script>


<select id="selectSubject"> //where the dynamic dropdown is show
<option>Choose assigned subject</option>
</select>

  //additional info that may contribute to the problem
 <form>
<input type="hidden" name ="subjectFolder" id="uploadFile">
  <input type="file" name="imageFile">
  <input type="button" value="Upload File" onclick="google.script.run.withSuccessHandler().upload(this.parentNode)">
</form>

附加信息是我计划将下拉列表的值用作&lt;input type="hidden" name ="subjectFolder" id="uploadFile"&gt; 中的参数using document.getElementById('uploadFile').value = "value of selected option in dropdown"

编辑 更新我也试过了,但没用

var subjects = document.getElementsByTagName("select").getElementById('selectSubject').value;
document.getElementById('uploadFile').value = subjects;

** 编辑** 更新 我已经尝试了 ravishankar chavare 的方法,但仍然无法正常工作。

 var e = document.getElementById("selectSubject");
 var selected_value = e.options[e.selectedIndex].value;
 document.getElementById('uploadFile').value = selected_value;

** 编辑** 更新

 var e = document.getElementById("selectSubject");
 var selected_value = e.options[e.selectedIndex].value;
 document.getElementById('uploadFile').value = selected_value;
 console.log(selected_value);

控制台仅打印“选择分配的主题”,因为它是默认选择的,但是当我删除 &lt;option&gt; Choose assigned subject &lt;/option&gt; 时,默认选择是基于下拉列表的 javascript 数组值之一,但控制台无法打印它的值。

【问题讨论】:

  • 你不应该给出冲突的id。它应该是唯一的
  • 是的,我很清楚这就是为什么我要求其他方法来获取下拉列表中的选定值

标签: javascript html google-apps-script web-applications getelementbyid


【解决方案1】:

var select = document.getElementById("selectSubject");
var opt = 'youreemailvalue';
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);

function SetSelectedValue() {
var e = document.getElementById("selectSubject");
 var selected_value = e.options[e.selectedIndex].value;
 document.getElementById('uploadFile').value = selected_value;
 alert('value set to uploadfile')
}
<select id="selectSubject" onchange="SetSelectedValue();"> 
<option>Choose assigned subject</option>
</select>

 <form>
<input type="hidden" name ="subjectFolder" id="uploadFile">
  <input type="file" name="imageFile">
  <input type="button" value="Upload File" onclick="google.script.run.withSuccessHandler().upload(this.parentNode)">
</form>

以下代码对我有用

var e = document.getElementById("selectSubject");
var selected_value= e.options[e.selectedIndex].value;

selected_value获取用户选择的值

在这里工作的 Jsfiddle 示例 https://jsfiddle.net/0yfdc51g/

【讨论】:

  • 首先检查下拉值和文本是否填充
  • 它确实填充了我的下拉菜单。此外,当我尝试手动将下拉列表的值放入 document.getElementById('uploadFile').value = "manual_selected_value";它有效
  • 表示当前您正在从菜单中获取值,但是当您设置为隐藏字段时,如果我是对的,则它不起作用 $("#uploadFile").val(selected_value);尝试在控制台打印它
  • 是的,它只打印选择分配的主题,不能打印从 javascript 生成的其他选项
  • 我创建了一个简单的 jsfiddle,其中菜单通过 javascrip 动态填充,当用户选择该项目时,它会自动将值设置为 uploadFile 检查此处jsfiddle.net/0yfdc51g
【解决方案2】:

您可以使用document.querySelector。如果要全选select#selectSubject,请使用document.querySelectorAll

console.log( document.querySelector( 'select[id=selectSubject]' ) );
console.log( document.querySelectorAll( 'select[id=selectSubject]' ) );
<div id="selectSubject"></div>

<select id="selectSubject" class="select"></select>
<select id="selectSubject"></select>

【讨论】:

  • 通过放置一个 selectSubject 的 div id,它会将下拉菜单之外的选项以纯文本形式显示出来。我也尝试添加 class="select" 并使用 document.querySelector 它也不起作用
  • 这些只是具有冲突 ID 的元素以及如何获取元素的示例。您能否提供一个您所面临的conflicting ID 示例?
  • 上面已经显示了。 selectSubject 是冲突的 ID,因为它用于从 javascript 获取附加选项以填充 select 中的选项。我也想在不重复使用它的 ID 的情况下获取选定的值
猜你喜欢
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多