简体   繁体   English

如何获取javascript以查看更新的选择值?

[英]How do I get javascript to see updated select value?

I have a page with about 100 dropdown menus that I need to pass to another. 我有一个页面,有大约100个下拉菜单,我需要传递给另一个。 So, I've put everything in an array that I'm sending via javascript. 所以,我把所有内容放在我通过javascript发送的数组中。 However, I'm not sure how to get the javascript to see the changed values of the dropdowns before sending. 但是,我不确定如何让javascript在发送之前查看下拉列表的更改值。 I mocked up some code to give you an idea of the problem. 我嘲笑了一些代码,让你了解问题。 It only sends the value of the dropdown box at the time the page is initialized. 它仅在页面初始化时发送下拉框的值。 Any help would be appreciated. 任何帮助,将不胜感激。

<select id="mydropdown">
  <option value="Milk">Fresh Milk</option>
  <option value="Cheese">Old Cheese</option>
  <option value="Bread">Hot Bread</option>
</select>

<script>
var data = new Array();

data[0] = document.getElementById("mydropdown").value;
</script>


<form name="data" method="POST" action="passdata1b.php">
<input type="hidden" name="data">
</form>
<script>
function sendData()
{
  // Initialize packed or we get the word 'undefined'
  var packed = "";
  for (i = 0; (i < data.length); i++) {
    if (i > 0) {
      packed += ",";
    }
    packed += escape(data[i]);
  }
  document.data.data.value = packed;
  document.data.submit();
}
</script>
<h1>This is what the array contains:</h1>
<ul>
<script>
  for (i = 0; (i < data.length); i++) {
    document.write("<li>" + data[i] + "</li>\n");
  }
</script>
</ul>
<a href="javascript:sendData();">Go to passdata1b.php</a>

Why can't you put this logic: 为什么你不能把这个逻辑:

var data = new Array();
data[0] = document.getElementById("mydropdown").value;

In your sendData() function? sendData()函数中?


Comment if you need an example, but this should be a pretty easy fix. 如果您需要一个示例,请注释,但这应该是一个非常简单的修复。 That way, when you click the link and run sendData() , it will parse the mydropdown value..instead of doing it on page load. 这样,当您单击链接并运行sendData() ,它将解析mydropdown值...而不是在页面加载时执行此操作。

Sam's answer was good, except.. 山姆的答案很好,除了......

data[0] = document.getElementById("mydropdown").value;

..that won't work since it's a dropdown menu. ..这不会起作用,因为它是一个下拉菜单。 Instead get the value of the selected option. 而是获取所选选项的值。 Use this instead: 请改用:

var zeData = document.getElementById("mydropdown");
data[0] = zeData.options[zeData.selectedIndex].value;

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

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