只需点击一下即可打开3个不同的谷歌搜索

时间:2017-10-27 00:03:11

标签: javascript onclick

我正在寻找一种方法,只需点击一下即可使用三种不同的关键字进行三次Google搜索。

这是我到目前为止所得到的:

<form action="https://google.com/search" method="get" target="_blank">
<input type="text" id="q1" name="q" placeholder="first" required /><br>
<input type="text" id="q2" name="q" placeholder="second" required /><br>
<input type="text" id="q3" name="q" placeholder="third" required /><br>
<input type="submit" value="Google Search" />
</form>

它会打开一个新标签,其中包含对所有三个关键字的Google搜索。 但我希望它打开三个不同的标签/网站/框架...每个标签/网站/框架搜索一个关键字。我想有一种方法可以用js来解决这个问题,但我对Java脚本几乎一无所知。 我需要这样的东西: onsubmit = window.open(“search1”); window.open( “搜索2”); window.open( “search3”);

希望你能帮助我。

格尔茨

2 个答案:

答案 0 :(得分:0)

function openNewTab(url){
  var redirectWindow = window.open(url, '_blank');
    redirectWindow.location;
}
document.getElementById("asd").addEventListener("click", submit);
function submit(){
var a = document.getElementById('q1').value;
var b = document.getElementById('q2').value;
var c = document.getElementById('q3').value;
var url = "http://google.com/search?q=";
 openNewTab(url+a);
 openNewTab(url+b);
 openNewTab(url+c);

}
<form>
<input type="text" id="q1" name="q" placeholder="first" required /><br>
<input type="text" id="q2" name="q" placeholder="second" required /><br>
<input type="text" id="q3" name="q" placeholder="third" required /><br>
<input type="submit" id="asd" value="Google Search" />
</form>

这完全基于用户的浏览器设置,是否允许通过脚本打开新选项卡。以上代码仅在浏览器中允许使用弹出窗口时才有效。我认为没有其他方法可以解决它

要在您的浏览器上进行测试(假设您正在运行chrome),您可以在浏览器中更改设置chrome://settings/content/popups并允许弹出窗口

答案 1 :(得分:0)

我有一个解决方案......不是很优雅,但它有效(有点)。

<form id="form1" action="https://google.com/search" method="get" target="_blank">
   <input type="text" id="q1" name="q" placeholder="first" required /></form><br>
<form id="form2" action="https://google.com/search" method="get" target="_blank">
   <input type="text" id="q2" name="q" placeholder="second" required /></form><br>
<form id="form3" action="https://google.com/search" method="get" target="_blank">
   <input type="text" id="q3" name="q" placeholder="third" required /></form><br>

<button onclick="document.getElementById('form1').submit();
document.getElementById('form2').submit();
document.getElementById('form3').submit();"> Search </button>