[英]TypeError: document.getElementById(…) is null - Error Message
[英]document.getElementById is NULL error
我有一個函數監聽onkeyup事件,該onkeyup檢查以查看一個框是否有值,如果不為空,則應提取elementID的值,elementID是從另一頁調用的電話號碼。 它采用該值並將其插入到發送到另一個elementID的html代碼段中。 以下代碼是功能和相關代碼。
<script language="javascript">
function monthlycheck() {
var mnthchk = document.getElementById("mdamountBox");
var cancelPhone = document.getElementById("cancelphoneLabel");
document.getElementById("cancelphonelistLabel").value = cancelPhone; <--gives the is null error
if(mnthchk.value!=""){
var newHTML = "<span style='color:#24D330'> Your Monthly pledge in the amount of $__ is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'> </label>";
" </span>";
document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML;
}
}
</script>
<label id="mnthlychkdiscoLabel"> </label> <---this is where the final data is to be displayed
<label id="cancelphoneLabel">1-800-555-1111</label> <--- this is the phone number pulled from another page,
加載時,所有數據都放在一個頁面中,但是使用smarty模板寫在單獨的頁面中。 我不斷收到標題錯誤,並嘗試了多種解決方法,但是即時通訊幫助不勝感激。
這是一個jfiddle http://jsfiddle.net/rn5HH/
我認為您正在嘗試在創建該ID之前對其進行訪問。 而且.value
僅用於輸入。 看起來您正在創建標簽,因此應改用innerHTML。
像這樣:
<script language="javascript">
function monthlycheck() {
var mnthchk = document.getElementById("mdamountBox");
var cancelPhone = document.getElementById("cancelphoneLabel");
if(mnthchk.value!=""){
var newHTML = "<span style='color:#24D330'> Your Monthly pledge in the amount of $__ is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'> </label></span>";
document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML;
document.getElementById("cancelphonelistLabel").innerHTML = cancelPhone; //<--gives the is null error
}
}
</script>
<label id="mnthlychkdiscoLabel"> </label> <---this is where the final data is to be displayed
<label id="cancelphoneLabel">1-800-555-1111</label> <--- this is the phone number pulled from another page,
我不是100%地嘗試做的事情,所以我只是想讓它工作。 您可以簡化很多事情。 如果可以創建一個jsfiddle來更清楚地顯示它,這將有所幫助。
編輯:
修復了小提琴,使其正常工作並顯示了電話號碼: 更新了小提琴
那是應該做的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.