我正在尝试从“create_name()”获取数据并将结果写入
<p id="name"> </p>
更新变量名称以显示新版本。
然而,我在诚实中有点迷失,而是以“未定义”作为名称。代码显然会到达此部分然后失败:
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
您可以在此处查看我想要完成的整个部分的代码。
我希望我解释清楚。
有问题吗?随意问,所有的帮助表示赞赏!
init(); // lets start the inital func
function init(){ // Do some asking shit
var name = prompt('hello, what is your name?'); // ask for the name
document.write('<p id="name">hello ' + name + ' </p>');
document.write('<p id="user" >1 - create a username</p>');
document.write('<p id ="play" >2 - Play Quiz<p>');
document.write('<p id="reload">3 - quit and reload<p>');
Writedata(name); // tell our witer to write the name
}
function menu_function() {
var choice = prompt("hello " + name + " please select an option from the list to your left");
switch (choice) {
case "1": opt1_function(); break;
case "2": opt2_function(); break;
case "3": reload_method(); break;
default: menu_function(); break;
}
}
menu_function();
function opt1_function() {
alert(name + " you have selected option 1");
create_name();
}
function create_name() {
var forename = prompt("what is your forename?");
var surname = prompt("what is your surname?");
var username = alert("hello " + forename + " " + surname);
var string1 = forename.substring(0 , 1);
var create_name = alert(string1 + surname);
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
答案 0 :(得分:1)
停止为变量分配alert()函数返回值,alert()没有返回值look at this alert definition
更改此行
var create_name = alert(string1 + surname);
到
alert(string1 + surname);
var create_name = string1 + surname;
答案 1 :(得分:0)
将create_name()函数中的数据更改为:
function create_name() {
var forename = prompt("what is your forename?");
var surname = prompt("what is your surname?");
var username = alert("hello " + forename + " " + surname);
var string1 = forename.substring(0 , 1);
var create_name = string1 + surname;
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
现在它有效!