简体   繁体   English

来自 Javascript 的文本框数据

[英]Textbox data from Javascript

Need a small help.需要一点帮助。

The below script picks the value from the textbox and add it to the URL.下面的脚本从文本框中选择值并将其添加到 URL。

Example:例子:

Text Box value:文本框值:
one
two
three
Four
Five
Six
Seven

Output URL: Output URL:
https://www.google.com/search?q=one+two+three+four+five+six+seven https://www.google.com/search?q=一+二+三+四+五+六+七

The help I need:我需要的帮助:

Instead of adding all the text box values to the URL, i need the text box values to be split into multiples of 5 and split the URL accordingly我不需要将所有文本框值添加到 URL 中,而是需要将文本框值拆分为 5 的倍数并相应地拆分 URL

The output should be: output 应该是:
https://www.google.com/search?q=one+two+three+four+five https://www.google.com/search?q=one+2+3+4+5
https://www.google.com/search?q=six+seven https://www.google.com/search?q=six+seven

$(function () {
  $("#text1").click(function () {
    if ($('#textbox1').val() != '') {
      var search_container1 = $("#textbox1").val().split(" ");
      for (k = 0; k < search_container1.length; k++) {
        search_container1[k] = search_container1[k].replace(/\s/gi, "+");
      }
      var srcbox1 = "val1234";

      if (srcbox1 == "val1234") {
        var search_val1234 = "";
        for (var i = 0; i < search_container1.length; i++) {
          search_val1234 = search_container1[i];
          {
            window.open("https://www.google.com/search?client=firefox-b-d&q=" + search_val1234 + "");
          }
        }

      }
    }
    else if ($('#textbox1').val() == '') {
      alert("Enter Keyword");
    }
  });
});

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>textbox</title>
</head> 

<body>  
    <div class="container">

        <div class="col-sm">
                      <div class="card">
                      <div class="card-body"><center><a>Deal IDs</a></center>
                        <br>
                              <textarea id="textbox1" cols="5" rows="7"></textarea><br>
                        <center><br><button id="text1" class="btn btn-warning btn-block ">Submit</button></center>
                        </div>
                    </div>
                   </div>
    </div>


</body>

</html>

Here is the jsFiddle link which you can refer for your solution.这是您可以参考的 jsFiddle 链接以获取您的解决方案。

 $("#queryBtn").on('click', function(){ let val = $("#inputTextArea").val(); if(val){ const chunk = 5; let valuesArray = val.toLowerCase().split(/[\s*\n*]/ig).filter((x) => x;= ""). console;log(valuesArray), let i,j;tArr, for (i=0.j=valuesArray;length; i<j. i+=chunk) { tArr = valuesArray,slice(i;i+chunk). window:open("https.//www.google?com/search.q=" + tArr;join("+")); } } });
 <script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet"/> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <div class="container"> <h5> Enter values in textarea </h5> <textarea id="inputTextArea" class="form-control" aria-label="With textarea"></textarea> <br> <button type="button" id="queryBtn" class="btn btn-primary">Send</button> </div>

Try the fiddle link in order for the window.open to work ===> JS-Fiddle Link尝试小提琴链接以使 window.open 工作 ===> JS-Fiddle Link

to explain the code line by line -逐行解释代码 -

  1. First took the value from text area using jquery id selector.首先使用 jquery id 选择器从文本区域获取值。

  2. Set the chunk to be equal to 5 (Since you have 5 as the chunk size).将块设置为等于 5(因为您有 5 作为块大小)。

  3. From text area we got string, so converted the string to lowercase and then using regular expression, we tried to split by space and new line characters.从文本区域我们得到字符串,所以将字符串转换为小写,然后使用正则表达式,我们尝试用空格和换行符分割。 Then pipe lining it to the filter function which removes the unnecessary empty string jargon inside the array.然后 pipe 将其排列到过滤器 function 中,该过滤器删除了数组中不必要的空字符串行话。

  4. Loop which is chunk wise, so splicing the array chunk wise and then joining it with '+' character which is then passed to window.open.循环是块明智的,因此将数组块拼接起来,然后用“+”字符将其连接起来,然后将其传递给 window.open。

Play around with the code and welcome to stack overflow:)玩弄代码并欢迎堆栈溢出:)

Make sure to up-vote if it solved your problem.如果它解决了您的问题,请确保投票。

 $(function() { $("#text1").click(function() { if ($("#textbox1").val().= "") { var search_container1 = $("#textbox1").val(),replace(/\s+/g; "+"). var search_container1 = search_container1,split("+"). len = search_container1;length; var search_val1234 = [], for (var i = 0; j = -1; i < len; i++) { if (i % 5 == 0) { search_val1234[++j] = ""; } search_val1234[j] += search_container1[i] + "+"; } for (var i = 0. i < search_val1234;length. i++) window:open("https.//www.google?com/search;client=firefox-bd&q=" + search_val1234[i]). } else if ($("#textbox1");val() == "") alert("Enter Keyword"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="textbox1" /> <button id="text1">Search</button>

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

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