创建一个接受用户输入的表单,并在页面加载时将其添加到句子中

色衣

我正在尝试复制类似的内容,该页面接受用户输入的名称,例如名称,然后在下一页的句子中显示该名称

我已经编写了类似的代码,但是与我需要的不匹配。任何帮助或教程对此将不胜感激。

添加完整代码。我希望能够在秒钟提示中使用我的名字,例如“ Greg type a形容词”。但是我发现很难。

// List of prompts for the user
var prompts = [
	'Type your name',
	'Type an adjective',
	'Type a noun'
   ];

var answers=[];
// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function() {
  //if there's no answer in the form
  if (currentPrompt != 0){
    answers.push($('input').val());
  }
	// if there is a next prompt
	if (currentPrompt < prompts.length) {
		// put first prompt in all html elements with class 
		$('.prompt').html(prompts[currentPrompt] + '<br><input type="text">');
		// move the next prompt into variable currentPrompt 
		 
     currentPrompt = currentPrompt + 1;
	}

  


	//or else if we're at the end of the array
	else {
		// put a new message into the html.
		showFinal();
	
	}
}

//puts user answers into HTML
var showFinal = function() {
  $('.prompt').html('This is the story of <span class="fill">'+answers[0]+'</span> and the <span class="fill">'+answers[1]+'</span> <span class="fill">'+answers[2]+'</span>.');
  //and then hide the button
  	$('button').hide();
}
// run nextPrompt function when button is clicked
$('button').click(function() {
	nextPrompt();
});

// Show the first prompt as soon as js loads
nextPrompt();
body{
  text-align: center;
  font-family: 'Fjalla One';
  font-size: 20px;
  background: #e6eaf0;
  }
  
button{
  margin: 40px;
  }
  
input {
  font-size: 24px;
  }
  
.fill {
  background: white;
  color: red;
  border-bottom: 2px black solid;
  font-family: 'Shadows Into Light';
  padding: 0 6px;
  margin: 4px;
  }
<!DOCTYPE html>

<head>
<link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Shadows+Into+Light" rel="stylesheet">
</head>

<body>
<div class="prompt"></div>
<button>Next</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

未定义

嗯...有趣。

我设法按照您指定的方式在下面的代码段中获取代码。我添加{name}到第二个提示。调用该提示时,用户已经在上一个answers[0]设置中输入了他们的名称=>并且是用户名。因此,我置于ifnextPrompt()检查当前是否提示是第二个。如果是的话,我更换{name}prompts[1]answers[0]相当愚蠢,但我希望您觉得它有用。

// List of prompts for the user
var prompts = [
	'Type your name',
	'{name}, type an adjective',
	'Type a noun'
   ];

var answers=[];
// Keep track of current prompt we're on
var currentPrompt = 0;

// A function that will call the next prompt
var nextPrompt = function() {
  //if there's no answer in the form
  if (currentPrompt != 0) {
    answers.push($('input').val());
  }    
	// if there is a next prompt
	if (currentPrompt < prompts.length) {
		// put first prompt in all html elements with class
    if (currentPrompt == 1) {
      prompts[1] = prompts[1].replace("{name}", answers[0]);
    }
		$('.prompt').html(prompts[currentPrompt] + '<br><input type="text">');
		// move the next prompt into variable currentPrompt 
		 
     currentPrompt = currentPrompt + 1;
	}

  


	//or else if we're at the end of the array
	else {
		// put a new message into the html.
		showFinal();
	
	}
}

//puts user answers into HTML
var showFinal = function() {
  $('.prompt').html('This is the story of <span class="fill">' + answers[0] + '</span> and the <span class="fill">' + answers[1] + '</span> <span class="fill">' + answers[2] + '</span>.');
  //and then hide the button
  	$('button').hide();
}
// run nextPrompt function when button is clicked
$('button').click(function() {
	nextPrompt();
});

// Show the first prompt as soon as js loads
nextPrompt();
body {
  text-align: center;
  font-family: 'Fjalla One';
  font-size: 20px;
  background: #e6eaf0;
}
  
button {
    margin: 40px;
}
  
input {
    font-size: 24px;
}
  
.fill {
    background: white;
    color: red;
    border-bottom: 2px black solid;
    font-family: 'Shadows Into Light';
    padding: 0 6px;
    margin: 4px;
}
<!DOCTYPE html>
<html>
<head>
    <link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Shadows+Into+Light" rel="stylesheet">
</head>

<body>
    <div class="prompt"></div>
    <button>Next</button>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Bash中将多个用户输入添加到一个变量中

如何在Pyqt5 Python中打开另一个窗口并接受用户输入

如何创建一个Siri Intent来接受用户传递的输入

创建一个按钮以将多个类添加到多个按钮,并在单击重置时将其删除

Android,接受用户的值并在下一个活动中显示它们

如何创建一个类并将其添加到for循环的列表中,以从控制台接收输入

LinkedList-创建函数,该函数接受一个值,创建一个节点,并将其添加到列表末尾

如何接受用户输入,但将其全部重定向到另一个文件?

如何接受用户输入以创建类的另一个实例?(即根据用户规范添加员工?)

创建一个新项目并将其添加到我的邻居的列表中

我正在尝试在python中创建一个程序,以接受用户提供的输入并告诉他们是否未成年人

将事件侦听器作为部分页面的一部分加载时,如何将其添加到表单中?

获取用户输入并将其添加到阵列VBA的最后一个索引

创建一个.bat文件,该文件在命令提示符下接受用户输入并将其保存到文本文件中

要求用户在a类中输入2个整数并将其添加到b类中并在C类中具有显示功能的最佳方法是什么?

接受用户输入的单词的第一个字母,并在Java中反复将其添加到末尾

创建一个zip文件,并使用maven将其添加到war文件中

接受用户输入以在Android中创建“ for”循环

按键创建一个新对象并将其添加到列表中

编写一个程序,接受用户的输入并将其与文件中的单词进行比较

如何编写一个接受参数并将其添加到数组的函数?

使用多个块在页面加载时将文本添加到类中的第一个元素

如何创建一个函数,从一个 QTreeWidget 中删除一个项目并将其添加到另一个?

如何创建一个接受用户输入的子程序?

我创建了一个登录页面,我需要将这些内容添加到我的用户名中。我如何在颤振中验证我的表单

创建和托管接受用户输入并将其添加到 sql 数据库的 python webapp 的最佳方法?

Java 不接受用户输入的第一个变量

C# 创建一个接受用户输入的列表,然后在某个值范围内打印列表中的数字。(请解释它的工作原理和原因)

如何在 Discord 中创建一个接受用户输入的弹出窗口?