简体   繁体   English

如何使用JavaScript询问用户输入并将其分配给数组的值

[英]How to use JavaScript to ask input from User and assign it to values of arrays

Hi i use this code to view multiple URLs in an iframe , now i want to make a script in JavaScript which will ask from user to input urls and will assign URLs in arrays in the below code. 嗨,我使用此代码在iframe查看多个URL,现在我想在JavaScript中创建一个脚本,该脚本将要求用户输入URL,并在下面的代码中以数组形式分配URL。

 var array = ["https://www.google.com", "https://codepen.io/", "https://www.amazon.in", "http://www.stackoverflow.com"]; var cnt = 0; $('button').click(function() { $('iframe').attr('src', array[cnt]); cnt++; if (cnt == array.length) { cnt = 0; } }); 
 iframe { width: 400px; height: 400px; } 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <button type="submit">Next Iframe</button> <iframe></iframe> 

Try following code 尝试以下代码

 var array = ["https://www.google.com", "https://codepen.io/", "https://www.amazon.in", "http://www.stackoverflow.com"]; var cnt = 0; $('button').click(function() { $('iframe').attr('src', array[cnt]); cnt++; if (cnt == array.length) { cnt = 0; } }); function addtoArrayFunc(){ var inputVal = $("#inputBox").val(); array.push(inputVal); } 
 iframe { width: 400px; height: 400px; } 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <button type="submit">Next Iframe</button> <iframe></iframe> <br> <input type="text" value="" placeholder="Enter the URL" id="inputBox"> <button onClick="addtoArrayFunc()">Add to array</button> 

Create a form, and an input. 创建一个表单和一个输入。 Then read input when user clicks on button/interact in some other way. 然后当用户单击按钮/以其他方式进行交互时读取输入。

 var array = ["https://www.google.com", "https://codepen.io/", "https://www.amazon.in", "http://www.stackoverflow.com"]; var cnt = 0; $('button').click(function() { var userurl = $('input').val(); $('iframe').attr('src', array[cnt] = userurl); cnt++; if (cnt == array.length) { cnt = 0; } }); 
 iframe { width: 400px; height: 400px; } 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <iframe></iframe> <!-- don't reload page on --> <form action="javascript:void(0);"> <label> Get URL <input type="text"></input> </label> <button type="submit">Go </button> </form> 

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

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