javascript - Unable to Returning function to main HTML body javascript
问题描述
I call the function from a click of the submit button here, and retrieving the entered text works fine.
<button class="btn btnEmail" id="emailBtn" onclick="emailUser.getEmailInput();">Submit</button>
<input type="button" value="submit" onclick="writeMail(this.emailInput)" />
It seems the function is not able to return the value of emailInput to html main body as I cannot use it with my writemail function.
the getEmail function:
let emailUser = {
getEmailInput: function() {
this.emailInput = document.getElementById('emailInput').value;
document.getElementById("emailOutput").innerHTML = this.emailInput;
return(this.emailInput);
}
};
the writeMail function:
function writeMail(email) {
console.log(email);
localStorage.setItem('email',email);
let theEmail = localStorage.getItem('email');
console.log(theEmail);
}
解决方案
this
inonclick="writeMail(this.emailInput)"
将引用元素本身。我:e input with type button
。
而是使用传递其他输入的值document.getElementById('emailInput').value)
。
function writeMail(email) {
console.log(email);
localStorage.setItem('email', email);
let theEmail = localStorage.getItem('email');
console.log(theEmail);
}
<input type="email" id="emailInput">
<input type="button" value="submit" onclick="writeMail(document.getElementById('emailInput').value)">
推荐阅读
- excel - 计算时的 VBA 问题
- reactjs - React - 使用 Ref 获取 DOM 元素 TEXT 值
- python - 将数据块火花笔记本内的数据帧中的数据存储到 S3 存储桶
- c++ - 在 libpcap 回调中获取数据包号
- android - 从 v1 到 2 的 Apollo GraphQL 迁移
- php - 在 php 8.x 中使用父函数访问子属性的问题
- elasticsearch - Kibana 7.11 - 索引模式导出未显示任何字段
- python - 实时处理程序将新数据从吉时利数字万用表附加到 csv 文件而不覆盖数据
- python - `re.Pattern.search()` 中的 UnicodeDecodeError
- c++ - 如果 operator!=() 使用 operator==() 的否定,我应该内联运算符 == 和 !=