2013-05-13 95 views
0

我有一個函數,用於偵聽onkeyup事件,onkeyup檢查是否有一個框有一個值,如果不是空白,應該拉一個elementID的值,這是從另一個調用的電話號碼頁。它接受該值並將其插入到發送給另一個elementID的html代碼片段中。下面的代碼是功能和相關的代碼。加載,但使用Smarty模板是寫在單獨的頁面時document.getElementById是NULL錯誤

<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'>&nbsp;</label>"; 
     " </span>"; 

     document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML; 
    } 
} 

</script> 

<label id="mnthlychkdiscoLabel">&nbsp;</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, 

所有的數據都在一個頁面拉到一起。我不斷收到有關錯誤,並嘗試了許多不同的事情來解決它,但即時通訊任何幫助非常感謝。

這裏是一個jfiddle http://jsfiddle.net/rn5HH/

+0

另一頁?你可以訪問另一個頁面的元素? – 2013-05-13 21:43:14

+0

你可以扔在小提琴嗎? – samayo 2013-05-13 21:44:30

回答

1

我認爲你正試圖創建真實之前訪問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'>&nbsp;</label></span>"; 

    document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML; 

    document.getElementById("cancelphonelistLabel").innerHTML = cancelPhone; //<--gives the is null error 

    } 
} 
</script> 

<label id="mnthlychkdiscoLabel">&nbsp;</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來更清晰地顯示它,這將有所幫助。

編輯:

固定小提琴所以它的工作原理,並顯示電話號碼: updated fiddle

難道這就是它應該做的?

+0

value屬性不僅僅用於輸入。大多數表單控件可能具有值屬性(並且都可能具有值屬性)以及其他一些元素(如LI和PARAM)。 – RobG 2013-05-13 22:45:53

相關問題