简体   繁体   English

将用户输入值传递给随机文本字符串

[英]Pass user input value to random text string

I am trying to create a topic generator. 我正在尝试创建一个主题生成器。 I want to pass a user input value into a sentence. 我想将用户输入值传递给句子。 For example, I want a user to give me a keyword like Dog, then I want that user input to be passed as a variable to a random string of text. 例如,我希望用户给我一个像Dog这样的关键字,然后希望将该用户输入作为变量传递给随机文本字符串。 eg: Top 5 Ways to Clean A Dog 例如:清洁狗的五种方法

Here is what I currently have: 这是我目前拥有的:

var randomStrings = [
    "Top 5 Ways To insert user input here ",
    "hello 2",
    "hello 3",
    "hello 4",
    "hello 5",
    "hello A",
    "hello B",
    "hello C",
    "hello D",
    "hello E"  // Note: No comma after last entry
];
function RndMsg() {
  var msg = randomStrings[Math.floor(Math.random()*randomStrings.length)];
  document.getElementById('randomDiv').innerHTML = msg;
}

The HTML looks like this: HTML看起来像这样:

<form action="" method="post" onsubmit="return false">
 <h2>Select a keyword to optimize for</h2>
  <input placeholder="Keyword" type="text" id="keyword" name="user-input" required="">
  <input class="button" type="button" value="Generate" name="sessionid" onclick="RndMsg()"/>
</form>

<div >
 <p id="randomDiv"></p>
</div>

See the updated code below, 请参阅下面的更新代码,

 var randomStrings = [ "Top 5 Ways To insert user input here ", "hello 2", "hello 3", "hello 4", "hello 5", "hello A", "hello B", "hello C", "hello D", "hello E" // Note: No comma after last entry ]; function RndMsg() { var msg = randomStrings[Math.floor(Math.random()*randomStrings.length)]; var newMsg= ''; $.each(randomStrings, function(i, str){ if (str === msg){ newMsg = str + ' ' + $('#keyword').val(); randomStrings[i] = newMsg; return false; } }); document.getElementById('randomDiv').innerHTML = newMsg; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form action="" method="post" onsubmit="return false"> <h2>Select a keyword to optimize for</h2> <input placeholder="Keyword" type="text" id="keyword" name="user-input" required=""> <input class="button" type="button" value="Generate" name="sessionid" onclick="RndMsg()"/> </form> <div > <p id="randomDiv"></p> </div> 

Here is what I would do; 这就是我要做的;

<form action="" method="post" onsubmit="return false">
 <h2>Select a keyword to optimize for</h2>
  <input placeholder="Keyword" type="text" id="keyword" name="user-input" required="">
  <input class="button" type="button" value="Generate" name="sessionid" id="generate"/>
</form>

<div >
 <p id="randomDiv"></p>
</div>

and: 和:

var randomStrings = [ //random string to attach to
    "Top 5 Ways To",
    "hello 2",
    "hello 3",
    "hello 4",
    "hello 5",
    "hello A",
    "hello B",
    "hello C",
    "hello D",
    "hello E"  // Note: No comma after last entry
];

var generate = document.getElementById('generate'); // grab the button from the html
//when the button is clicked do this function
generate.onclick= function () { 
//since the keyword can change, get it every time the user clicks generate and he gets a new topic
  var keyword = document.getElementById('keyword').value;
  var msg = randomStrings[Math.floor(Math.random()*randomStrings.length)];
//when inserting data written by the user into the page DO NOT USE INNERHTML! they can insert code here! use text content instead.
  document.getElementById('randomDiv').textContent = msg + " " + keyword;
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM