简体   繁体   English

表单文本区创建一个 URL

[英]Form Text Area creates a URL

I have a pretty simple need here.我在这里有一个非常简单的需求。

I want to have a text area, that is populated with a line of ID's - each id will be on a new line.我想要一个文本区域,其中填充了一行 ID - 每个 ID 将在一个新行上。

How can I generate a URL from that?我怎样才能从中生成一个 URL?

12345
09876

Once submitted should generate to be https://xx.com/12345&09876提交后应生成为https://xx.com/12345&09876

I have been using this Form value creates a URL and have changed it to text area, but cant get each line break to be changed to the '&' in the url string.我一直在使用此表单值创建一个 URL并将其更改为文本区域,但无法将每个换行符更改为 url 字符串中的“&”。

Any help would be amazing!任何帮助都会很棒!

<script type="text/javascript">
    function goToPage() {
        var page = document.getElementById('page').value;
        window.location = "https://google.com/" + page;
    }
    
</script>
<textarea name="textarea" style="width:250px;height:150px;" id="page"></textarea>
<input type="submit" value="submit" onclick="goToPage();" />

You can split by \\n to get the characters in each line and join by & , then remove the last character:您可以通过\\n拆分以获取每行中的字符并通过&加入,然后删除最后一个字符:

 var url = txt.value.split("\\n").join("&").slice(0, -1); console.log(url);
 <textarea id="txt"> 12345 09876 </textarea>

In the case of a form:在表格的情况下:

 txt.addEventListener('input', function(){ form.action = txt.value.split("\\n").join("&").slice(0, -1); console.log(form.action); })
 <textarea id="txt"> 12345 09876 </textarea> <form id="form"> <button>Submit</button> </form>


In your particular case:在您的特定情况下:

 <textarea name="textarea" style="width:250px;height:150px;" id="page"></textarea> <input type="submit" value="submit" onclick="goToPage();" /> <script type="text/javascript"> function goToPage() { var page = document.getElementById("page").value.split("\\n").join("&"); alert(page); window.location = "https://google.com/" + page; } </script>

You can replace text in a string using the JavaScript replace function, as follows:您可以使用 JavaScript 替换函数替换字符串中的文本,如下所示:

var newString = oldString.replace(/(\r\n|\r|\n)/g, '&');

This line takes the string variable oldString (which should contain the data with all of the linebreaks) and changes all of the linebreaks to ampersands.这一行采用字符串变量 oldString(它应该包含所有换行符的数据)并将所有换行符更改为&符号。 It uses something called a Regular Expression to find all of the line breaks.它使用称为正则表达式的东西来查找所有换行符。

So for your HTML:所以对于你的 HTML:

<script type="text/javascript">
    function goToPage() {
        var page = document.getElementById('page').value;
        window.location = "https://google.com/" + page.replace(/(\r\n|\r|\n)/g, '&');
    }
    
</script>
<textarea name="textarea" style="width:250px;height:150px;" id="page"></textarea>
<input type="submit" value="submit" onclick="goToPage();" />

try splitting your text area by \\n by doing so:通过这样做尝试通过\\n拆分文本区域:

var split_text = your_text_area.split("\n"); // Replace your_text_area

The string which will be passed in the text area will be splitted by \\n (newline) and every line will be inserted in the split_text variable, which is an array.将在文本区域中传递的字符串将被\\n (换行符) split_text ,并且每一行都将插入到split_text变量中,该变量是一个数组。

Then, you can generate a URL from the array like this:然后,您可以从数组中生成一个 URL,如下所示:

var url = '';

for(let i = 0; i < split_text.length; i++)
{
   url += split_text[i];
   
   if((i+1) != split_text.length) // <-- Not the most optimized way to do it
   {
      url += "&";
   }
}

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

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