target =“_ blank”不会打开新窗口

时间:2018-06-02 23:32:48

标签: javascript html

target =“_ blank”没有打开新窗口,只导航到同一窗口中的新页面,如下所示:

<form onSubmit="return process();" target="_blank"> 
<input type="text" name="url" id="url"  placeholder="Product ID"> <input type="submit" value="Search">
</form> 
<script>
function process(){
    var url= "https://myurl.com/products/" + document.getElementById("url").value.trim();
    location.href=url; return false;
}
</script> 

1 个答案:

答案 0 :(得分:1)

当您单击该按钮时,代码会立即触发,因此您的代码将导航到另一个页面但在同一个选项卡中。

Severity Code Description Project File Line Suppression State Error CS1705 Assembly 'Quizzario.Data' with identity 'Quizzario.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.Extensions.Identity.Stores, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.Extensions.Identity.Stores' with identity 'Microsoft.Extensions.Identity.Stores, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' Quizzario.BusinessLogic C:\Users\zolty\source\repos\ProjectPwr\Quizzario\Quizzario.BusinessLogic\Factories\ApplicationUserDTOFactory.cs 19 Active 仅在表单具有将数据发送到的操作时才有效。

改为使用target="_blank"

window.open(url)
function process() {
  var url = "https://myurl.com/products/" + document.getElementById("url").value.trim();
  window.open(url);
  return false;
}

或者在您的流程函数中将网址指定给表单的action属性,然后移除<form onSubmit="return process();" target="_blank"> <input type="text" name="url" id="url" placeholder="Product ID"> <input type="submit" value="Search"> </form>

我将表单命名为return false

f
function process() {
  var url = "https://myurl.com/products/" + document.getElementById("url").value.trim();
  document.f.setAttribute('action', url);

}