搜索按钮target = _blank jQuery变量

时间:2019-06-21 14:57:33

标签: javascript jquery

我正在使用一段JavaScript将基于搜索表单中单选按钮的变量传递到另一个站点,我需要在新窗口/标签中打开外部链接。我没有写这段脚本,但是可以正常使用,但是当我尝试使用网上找到的任何方法时,它们要么破坏代码,要么只是返回网站的网址,而不是外部网址

我尝试使用window.open而不是window.location,但这似乎不起作用。

var pacSearchURL = "http://sclends.lib.sc.us/eg/opac/results?qtype=keyword&query=",
  mobilePacSearchURL = "http://sclends.lib.sc.us/eg/opac/results?qtype=keyword&query=",
  catalogSearchPlaceholder = "Search Catalog",
  websiteSearchPlaceholder = "Search Site";

jQuery(function($) {
  $('body').on('submit', '#searchform', function() {
    var searchType = $('input[name=searchType]:checked', this).val();
    if (searchType == "catalog") {
      var windwoWidth = $(window).width();
      if (windwoWidth <= 520) {
        //if we are at a mobile resolution...
        window.location = mobilePacSearchURL + encodeURIComponent($("#s").val());
      } else {
        window.location = pacSearchURL + encodeURIComponent($("#s").val());
      }
      return false;
    } else {
      return true;
    }
  }).on('change', 'input[name="searchType"]', function() {
    if (this.value === "site") {
      $("#s").attr('placeholder', websiteSearchPlaceholder);
    } else if (this.value === "catalog") {
      $("#s").attr('placeholder', catalogSearchPlaceholder);
    }
  });
}(jQuery));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="submit" class="search-submit"><i class="fa fa-search"></i></button>
<input class="screen-reader-text" type="submit" id="searchsubmit" value="search" />
<input id="site" class="search_rad" type="radio" name="searchType" value="site"><label for="site">Site</label>
<input id="catalog" class="search_rad" type="radio" name="searchType" value="catalog" checked="checked"><label for="catalog">Catalog</label>

我希望在新标签页中打开外部网址,并在选择站点单选按钮时返回target = _self

1 个答案:

答案 0 :(得分:0)

使用window.open()代替window.location

 window.open(mobilePacSearchURL + encodeURIComponent($("#s").val()));

https://www.w3schools.com/jsref/met_win_open.asp