我有一个功能,允许用户在输入字段中输入文本,这个文本出现在故事板上。
我试图了解如何将此文本连接到开关以及if \ else语句以使用输入字段而不是提示弹出窗口。
我试图使用一个空值的全局变量,每当用户输入一个新文本时,它应该将其值更改为等于用户的输入值,之后再将其设置为null,但它没有用。
// This function will show needed text on the story board:
var showText = function(text) {
$("#storyBoard").append(text + "<br />");
// When a total amount of text inside story board will be more then the size of a
// story board window, this script will automaticly scroll down every time when
// appending additional text.
var element = document.getElementById("storyBoard");
element.scrollTop = element.scrollHeight;
};
// This is a button click function. When user clicks the Enter button all the text
// from the input line appends to the story board.
$("#btn").click(function() {
var btnClick = $("input[name=myInput]").val();
$("#storyBoard").append(btnClick + "<br>");
$("input[name=myInput]").val("");
var element = document.getElementById("storyBoard");
element.scrollTop = element.scrollHeight;
});
var mission1 = function() {
if (userIn !== "") {
var story = userIn.toLowerCase();
switch (story) {
case "yes":
showText("Yes");
showText("Welcome aboard!");
selectChar();
showText("You met your old friend that for some reason starting to fight against you!");
break;
case "no":
showText("Your choise is no.");
break
default:
showText("default");
break;
}
} else {
// I think that here I can put a function that will somehow "wait" for user's
// input from the input field and then call the mission1() again...
// But if so, i will have to repeat this function every time, and I prefer no
// to repeat the same code.
};
mission1();
&#13;