【问题标题】:is there any way to show input value to an object in alert box?有没有办法在警报框中显示对象的输入值?
【发布时间】:2020-05-06 12:23:27
【问题描述】:

我正在尝试在警报框中显示一些输入值。基本上我想做一个基本的登录用户界面。当有人单击登录时,我想弹出一个带有输入值的警报框。我发现这非常简单,只需将 .value 添加到该元素即可。但是我想要另一个注册按钮,当有人按下按钮时,我想在警报框中显示类似 {{email: , characterNumber:},{pass: , characterNumber:}} 的内容。

我的代码是这样的

<div class="login-container"> <mwc-textfield id="emailText" type="email" class="email-input" label="email" helper="Insert email"> </mwc-textfield>

  <mwc-textfield id="passwordText" type="password" class="password-input" label="password" helper="Insert password">
  </mwc-textfield>
  <br />

  <mwc-button id="logIn" label="Log in"> </mwc-button>
  <mwc-button id="signUp" label="Sign up"> </mwc-button>
</div>

还有js代码

const login = document.querySelector("#logIn");
const signUp = document.querySelector("#signUp");
const textfield = document.querySelector('#emailText');
const emailVal = document.querySelector('#emailText').value;
const passVal = document.querySelector('#passwordText').value;

login.addEventListener("click", () => {
  alert("Your email adress: " + emailText.value + '\n' + "Your password: " + passwordText.value);
});

signUp.addEventListener("click", () => {
  alert(emailText.value);
});


let email = {
  adress: emailText.value,
  pass: passwordText.value,
}

我尝试提醒电子邮件值,但提醒框为空

但是当我点击按钮时,它只显示地址,没有来自输入的电子邮件或密码值。

我正在尽力解释,但英语不是我的母语,我希望有人能帮助我

【问题讨论】:

  • 你能分享整个代码吗?
  • 当然,我用整个代码编辑了我的帖子

标签: javascript object methods


【解决方案1】:

基本上,您不能有一个包含多个输入字段的警报框,然后您可以为不同的字段设置多个警报框。像下面这样:

let user = prompt('user');
let password = prompt('password');
const confirmText = `user: ${user} with the password: ${password}`
let confirmResult = confirm(confirmText);
console.log("confirmation status:", confirmResult);

另一种解决方案是根据您正在使用的 ui 库使用对话框、模式或弹出窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-28
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多