簡體   English   中英

document.getElementById為null

[英]document.getElementById is null

我在IE中使用了以下JavaScript,但在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

問題是您的input元素沒有id name更改為id ,或者也添加一個id

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

另外,如果您不想添加id ,則可以使用getElementsByName (跨瀏覽器效果不佳 ),或者可以使用jQuery屬性選擇器:

$("input[name='dropdown']").val("APPLICATION");

更新資料

我只是注意到您在問題中說它正在IE中運行。 這意味着您必須使用舊版本的IE,因為IE7及更低版本中的getElementById錯誤地通過nameid選擇元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM