javascript - document.getElementById 为 null

标签 javascript jquery dom getelementbyid

我有以下 JavaScript 在 IE 中工作,但在 Chrome 和 Firefox 上不起作用。 这是代码:

<script type="text/javascript">
jQuery(document).ready(function() {

jQuery('#applicationSelect').change(function() {
document.getElementById('dropdown').value = "APPLICATION";
});

});
</script>

<input type="hidden" name="dropdown" value="" />

当我查看 Firebug 时,代码出现错误:

document.getElementById("dropdown") is null
document.getElementById('dropdown').value = "APPLICATION";

我需要你的建议。预先感谢您。

最佳答案

如果您已经在使用 jQuery,为什么不使用它来设置您的输入值:

$('#dropdown').val('APPLICATION');

正如其他人指出的,您输入的 id 不是“下拉菜单”。

另请注意,在大多数用例中,可以使用 $ 代替 jQuery

关于javascript - document.getElementById 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307390/

相关文章:

javascript - Vega-lite:您如何自定义图例标签?

javascript - 为什么我网页上的 UL 中的链接和文本消失了?

javascript - 如何删除jstree中选定节点的子节点?

jquery - 预加载整个元素

html - <template> 中的 outerHTML 行为

javascript - 使用 Javascript 在表中设置复选框

javascript - Node 错误 : Cannot use import statement outside a module even though I'm not

javascript - 如何更改多个选定 html 值的名称?

javascript - 使用ajax从页面加载JS

javascript - 在 JavaScript 中将字符串解析为 HTML