来自 Javascript 的文本框数据

Textbox data from Javascript

需要一点帮助。

下面的脚本从文本框中选取值并将其添加到 URL。

示例:

文本框值:







输出URL:
https://www.google.com/search?q=one+two+three+four+five+six+seven

我需要的帮助:

我不需要将所有文本框值添加到 URL,而是需要将文本框值拆分为 5 的倍数并相应地拆分 URL

输出应该是:
https://www.google.com/search?q=one+two+three+four+five
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>

这是 jsFiddle link,您可以参考它来解决问题。

$("#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>

尝试 fiddle link 以便 window.open 工作 ===> JS-Fiddle Link

逐行解释代码-

  1. 首先使用 jquery id 选择器从文本区域获取值。

  2. 将块设置为等于 5(因为块大小为 5)。

  3. 从文本区域我们得到字符串,所以将字符串转换为小写,然后使用正则表达式,我们尝试用space和换行符分割。然后将其通过管道连接到过滤器函数,该函数删除数组中不必要的空字符串行话。

  4. 按块循环,因此按块拼接数组,然后用 '+' 字符连接它,然后传递给 window.open.

玩转代码,欢迎使用 stack overflow :)

如果它解决了您的问题,请务必投上一票。

$(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-b-d&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>