在Chrome/IE11/Firefox的隐藏空字段中设置一个值

Set a value in hidden null field in Chrome/IE11/Firefox

本文关键字:设置 一个 字段 IE11 Chrome Firefox 隐藏      更新时间:2023-09-26

我正在尝试在JavaScript的潜台词中设置一个值。假设Disptext有一些我想在reportSubtext中设置的值。这段代码在IE8中运行良好,但在Chrome/IE11/Firefox中不起作用。我收到错误,例如无法设置已经为空的对象的值。潜台词是一个隐藏的字段。这是潜台词的定义:

<input type="hidden" name="reportSubText" value=" c:out value='${reportSubText}' c:set var="reportSubText" value="${reportSubText}">
document.getElementById('SubText').value = DispText

document.getElementById('SubText').value 在执行时为 null。

请编辑您的答案并向我们提供所有 HTML。 据我了解,您正在尝试在下拉列表中设置值?下面的代码解释了如何做到这一点

<!doctype html>
<html>
  <body>
    <select id="list-1">
      <option id="opt-id-1" name="opt-1" value="val-1">Option 1</option>
      <option id="opt-id-2" name="opt-2" value="val-2">Option 2</option>
    </select>
  </body>
  <script>
    // first option (faster execution)
    var list = document.getElementById('list-1');
    var options = list.options;
    console.dir(options[0]);
    options[0].setAttribute('id', 'opt-id-1-CHANGED');
    options[0].setAttribute('name', 'opt-name-1-CHANGED');
    options[0].setAttribute('value', 'opt-value-1-CHANGED');
    options[0].innerHTML='Option 1 CHANGED';
  </script>
</html>
document.getElementById('SubText').value

这将返回 null,因为您的代码显示该元素没有 id。你应该尝试

document.getElementsByName('reportSubText')[0].value