Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中输入的值_Javascript_Input_Quotes - Fatal编程技术网

javascript中输入的值

javascript中输入的值,javascript,input,quotes,Javascript,Input,Quotes,如果我有一个这样的代码,带有引号和作者,我怎么能得到两个输入类型=文本框,在这里我可以写两个名字,然后将名字显示在引号中以代替随机用户 我认为您希望用从两个标记获得的字符串替换引号数组中的randomuser。在这种情况下,您可以获得这些输入的内容,如下所示: var user1 = document.getElementById("input1").value; var user2 = document.getElementById("input2").value; 现在,您可以使用St

如果我有一个这样的代码,带有引号和作者,我怎么能得到两个输入类型=文本框,在这里我可以写两个名字,然后将名字显示在引号中以代替随机用户


我认为您希望用从两个标记获得的字符串替换引号数组中的randomuser。在这种情况下,您可以获得这些输入的内容,如下所示:

var user1 = document.getElementById("input1").value;
var user2 = document.getElementById("input2").value;
现在,您可以使用String.replace替换这些输入值的randomuser1和randomuser2,如下所示:

//Sub in the usernames
var newquote = quotes[0].replace("(randomuser1)", user1); //quotes[0] can of course be any index
newquote = newquote.replace("(randomuser2)", user2);
然后可以显示newquote。请记住,您应该避免使用document.write。相反,您可以在页面上创建一个display div,然后将报价放入其中,如下所示:

var quotediv = document.getElementById("quoteDiv"); //get the div (or any other element) that you want to display the quote in
quotediv.innerHTML = newquote; //display your quote in the div.
编辑:

若要从较大的列表中随机选择引号,请将引号[0]更改为类似引号[Math.floorMath.random*quotes.length]的内容。要添加更多用户,您需要添加更多输入和更多替换语句。例如:

//Sub in the usernames
var ind = Math.floor(Math.random() * quotes.length); //select random quote
var newquote = quotes[ind].replace("(randomuser1)", user1);
newquote = newquote.replace("(randomuser2)", user2);
newquote = newquote.replace("(randomuser3)", user3);
newquote = newquote.replace("(randomuser4)", user4);
newquote = newquote.replace("(randomuser5)", user5);

对于更多的用户来说,这可以简化为for循环,但我会让你自己去弄清楚。

避免使用document.write尽可能多地使用填鸭式写作是允许的吗?@Atifmohammedameanuddin:既然OP在学习JS,我想这是允许的:但我可能错了here@harsha你能写一些快速的例子吗你想用authors数组中的名称替换字符串中的randomuser?如果我有一个包含100个引号和5个用户的列表,我该怎么做呢?:谢谢你的回答!!为你编辑了答案。谢谢!!!为什么div中总是显示第二个引号;您定义了两次数组,而不是用两个元素来定义它。这里是,见第1行。
//Sub in the usernames
var ind = Math.floor(Math.random() * quotes.length); //select random quote
var newquote = quotes[ind].replace("(randomuser1)", user1);
newquote = newquote.replace("(randomuser2)", user2);
newquote = newquote.replace("(randomuser3)", user3);
newquote = newquote.replace("(randomuser4)", user4);
newquote = newquote.replace("(randomuser5)", user5);