简体   繁体   English

在表单字段中输入文本,在提交时生成多个URL,并在新选项卡中打开链接

[英]Input text in form-field, generate multiple URLs on submit and open the links in new tabs

I'm looking to input text that is then used to generate multiple URLs and open each of them in a different tab. 我正在寻找输入文本,然后将其用于生成多个URL,并在不同的选项卡中打开每个URL。

http://jsfiddle.net/Gv5bq/1/ http://jsfiddle.net/Gv5bq/1/

    <input type="text" id="text" />
<input type="button" id="btn" value="Submit" onClick="javascript: window.open('http://www.mywebsite.com/print/' + document.getElementById('text').value);" />
<input type="button" id="btn" value="Submit" onClick="javascript: window.open('http://www.anywebsite.com/print/' + document.getElementById('text').value);" />

basically does what I need but just for one instead of multiple URLs. 基本上可以满足我的需要,但只需要一个而不是多个URL。

For example: Input: hello123 On submit open a tab for each of the following URLs For example www.mywebsite.com/print/ hello123 / and www.anywebsite.net/q= hello123 &sort 例如:输入: hello123在提交时,为以下每个URL打开一个选项卡,例如www.mywebsite.com/print/ hello123 /和www.anywebsite.net/q= hello123 &sort

You find a code example below 您可以在下面找到一个代码示例

HTML HTML

<input type="text" id="text" />
<input type="button" id="btn" value="Submit" onclick="openURLs(document.getElementById('text').value)"/>

JavaScript JavaScript的

function openURLs(url){
  var baseURLs = [`www.mywebsite.com/print/${url}/`, `www.anywebsite.net/q=${url}&sort`]

  baseURLs.forEach(function(u) {
    window.open(u);
  });

}

 function openURL(){ var txt = document.getElementById('text').value; var link1 = 'http://www.mywebsite.com/print/'+ txt; var link2 = 'http://www.anywebsite.com/q='+ txt + '&sort'; var i; for(i = 1; i < 3; i++){ if(i == 1) { window.open(link1, '_blank'); } else if (i==2) { //it will be execute after 5 seconds setTimeout(function(){ window.open(link2, '_blank'); }, 5000); } } } 
 <input type="text" id="text" /> <input value="Submit" type="button" onclick="openURL()"> 

I updated the fiddle check here . 我在这里更新了小提琴支票 It will give you result as you want. 它将为您提供所需的结果。 For Eg: If you enter hello123 below respected url's will be open 例如:如果您在下方输入hello123 ,则受尊重的网址将打开

  1. http://www.mywebsite.com/print/hello123 http://www.mywebsite.com/print/hello123
  2. http://www.anywebsite.com/q=hello123&sort http://www.anywebsite.com/q=hello123&sort

Please check Always Allow Pop-ups then use this code 请选中“始终允许弹出窗口”,然后使用此代码

<input type="text" id="text" />
<input type="button" id="btn" value="Submit" onClick="javascriptFun()" />
<script>
function javascriptFun(){
window.open('http://www.mywebsite.com/print/' + document.getElementById('text').value,'_blank');
window.open('http://www.mywebsite1.com/print/' + document.getElementById('text').value,'_blank');
window.open('http://www.mywebsite2.com/print/' + document.getElementById('text').value,'_blank');

}

</script>

在此处输入图片说明

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

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