最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert input value into a string - Stack Overflow

programmeradmin2浏览0评论

So basically I am having trouble converting an input value into a string. HTML:

<input type="text" placeholder="Email Ex: john321" id="grailedemail">
<input type="text" placeholder="Domain Ex: @gmail" id="graileddomain">

JS:

let email = document.getElementById("grailedemail").value;
let domain = document.getElementById("graileddomain").value;
let _ge = grailed_email.toString();
let _gd = grailed_domain.toString();


let randNum = Math.floor(Math.random() * 999999) + 1;
let emailAltered = `${_ge}+${randNum}${_gd}`;

I dont know if this is the right use of .toString method.

The output of the above returns this:

{ "email":"+388321", "pass":"password", }

Expected Output:

{ "email":"[email protected]", "pass":"password", } (I want to get whatever the user inputs) Where before the plus there is supposed to be the variable _ge or email And after the number, there is supposed to be the variable _gd or domain

So basically I am having trouble converting an input value into a string. HTML:

<input type="text" placeholder="Email Ex: john321" id="grailedemail">
<input type="text" placeholder="Domain Ex: @gmail." id="graileddomain">

JS:

let email = document.getElementById("grailedemail").value;
let domain = document.getElementById("graileddomain").value;
let _ge = grailed_email.toString();
let _gd = grailed_domain.toString();


let randNum = Math.floor(Math.random() * 999999) + 1;
let emailAltered = `${_ge}+${randNum}${_gd}`;

I dont know if this is the right use of .toString method.

The output of the above returns this:

{ "email":"+388321", "pass":"password", }

Expected Output:

{ "email":"[email protected]", "pass":"password", } (I want to get whatever the user inputs) Where before the plus there is supposed to be the variable _ge or email And after the number, there is supposed to be the variable _gd or domain

Share Improve this question edited Dec 2, 2018 at 18:58 Miguel Betancourt asked Dec 2, 2018 at 18:33 Miguel BetancourtMiguel Betancourt 11 gold badge1 silver badge5 bronze badges 5
  • 3 what is this grailed_email and grailde_domain ? – Code Maniac Commented Dec 2, 2018 at 18:39
  • 5 Why do you convert toString value that is already a string? – Smollet777 Commented Dec 2, 2018 at 18:51
  • At what event do you get those values? On submit, document.load, input.change? We need more context. – Smollet777 Commented Dec 2, 2018 at 18:57
  • 1 I'm confused by the question, HTMLInputElement.value is already a string. – stealththeninja Commented Dec 2, 2018 at 19:27
  • There is no way the code you have provided could produce the object you describe — you don't create an object at all!. It looks like a significant chunk of your code is missing. You need to provide a minimal reproducible example – Quentin Commented Dec 2, 2018 at 22:10
Add a ment  | 

2 Answers 2

Reset to default 1

Your problem is a typo.

The variable that contains the email is the email, and the variable that contains the domain is the domain, but you're using grailed_email and grailed_domain, and that's why the wanted data isn't in the string. Change your code to:

let grailed_email = document.getElementById("grailedemail").value; // Variable grailed_email fixed
let grailed_domain = document.getElementById("graileddomain").value; //  Variable grailed_domain fixed
let _ge = grailed_email.toString();
let _gd = grailed_domain.toString();


let randNum = Math.floor(Math.random() * 999999) + 1;
let emailAltered = `${_ge}+${randNum}${_gd}`;

try this way

function debug() {
  var email = document.getElementById("grailedemail").value;
  var domain = document.getElementById("graileddomain").value;
  var _ge = email.toString();  
  var _gd = domain.toString(); 

  let randNum = Math.floor(Math.random() * 999999) + 1;
  let emailAltered = `${_ge}+${randNum}${_gd}`;

  document.getElementById('debug').textContent = emailAltered;
}
<input type="text" value="Johndoe" placeholder="Email Ex: john321" id="grailedemail">
<input type="text" value="@gmail" placeholder="Domain Ex: @gmail." id="graileddomain">
<input type="button" onclick="debug()" value="Submit">
<p id="debug"></p>

发布评论

评论列表(0)

  1. 暂无评论