dongpao1921 2013-05-13 21:40
浏览 192
已采纳

document.getElementById是NULL错误

I have a function that listens for onkeyup event, that onkeyup checks to see if a box has a value and if not blank is supposed pull the value of an elementID which is a phone number called from another page. It takes that value and inserts it in to a snippet of html that gets sent to another elementID. the folowing code is the function and the associated code.

<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,

all the data is pulled together in a single page when loaded but is written in separate page using smarty templates. I keep getting the entitled error and have tried a number of different things to fix it but im stumped any help is greatly appreciated.

here is a jfiddle http://jsfiddle.net/rn5HH/

  • 写回答

1条回答 默认 最新

  • dongyan1936 2013-05-13 21:51
    关注

    I think you are trying to access that id before it's created. Also .value is only for inputs. It looks like you are creating a label, so you'd use innerHTML instead.

    Something like this:

    <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,
    

    I'm not 100% what you are trying to do, so i just tried to make it work. There are many things you could probably simplify in this. It would help if you could create a jsfiddle to show it more clearly.

    Edit:

    Fixed the fiddle so it works and shows the phone number: updated fiddle

    Is that what it's supposed to do?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)