lalaba20 2021-12-08 16:49 采纳率: 100%
浏览 44
已结题

javascript两个function冲突其中一个运行不了

#想要做两个表单然后输入信息提交后,会弹出对话框包含json形式的内容。本来的思路是写两个function分别添加addEventListener处理表单,但是发现排在后面的addEventListener会报错。Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

问题相关代码,请勿粘贴截图
//creat  a function that retrive Email input and Password input into a dialog box in JSON String
let getInfomation = function(){
    var inputEmailValue = document.getElementById("login-email").value;
    var inputPasswordValue = document.getElementById("login-pswd").value;
    let acccountInfomation =  '{"email" : "default"  , "Password" : "default" }'
    let accountObj = JSON.parse(acccountInfomation);
    accountObj.email = inputEmailValue;
    accountObj.Password = inputPasswordValue;
    acccountInfomation = JSON.stringify(accountObj);
    window.alert(acccountInfomation);

}


//creat  a function that retrive all 4 inputs into a dialog box in JSON String
let getTextInfomation = function(){
    var RecipientNameValue = document.getElementById("to-name").value;
    var RecipientEmailValue = document.getElementById("to-email").value;
    var emailSubjectValue = document.getElementById("email-subject").value;
    var emailTextValue = document.getElementById("email-text").value;

    let acccountInfomation =  '{"RecipientName" : "default"  , "RecipientEmail" : "default","emailSubject" : "default", "emailText" : "default" }'
    let accountObj = JSON.parse(acccountInfomation);
    accountObj.RecipientName = RecipientNameValue;
    accountObj.RecipientEmail =RecipientEmailValue ;
    accountObj.emailSubject = emailSubjectValue;
    accountObj.emailText = emailTextValue;
    acccountInfomation = JSON.stringify(accountObj);
    window.alert(acccountInfomation);

}
//add an EventListener to the log in button
// here I got a error says "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')", I tried to add "defer" attribute in <script>
// but unfortunately, it didn't work out at all. The second function works perfectly well without first function
document.getElementById("email-submit-btn").addEventListener("click", getTextInfomation );

//add an EventListener to the log in button
document.getElementById("e-login-btn").addEventListener("click", getInfomation );

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
我想要达到的结果

img

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-文盲老顾 2021-12-08 17:01
    关注
    
    document.getElementById("email-submit-btn") && document.getElementById("email-submit-btn").addEventListener("click", getTextInfomation );
     
    //add an EventListener to the log in button
    document.getElementById("e-login-btn") && document.getElementById("e-login-btn").addEventListener("click", getInfomation );
    

    确保你要添加监听的对象存在

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

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 创建了问题 12月8日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效