2013-05-13 92 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

相关问题