【问题标题】:Search Button target= _blank jquery variable搜索按钮目标= _blank jquery 变量
【发布时间】:2019-06-21 14:57:33
【问题描述】:

我正在使用一段 javascript 根据搜索表单中的单选按钮将变量传递到不同的站点,我需要在新窗口/选项卡中打开外部链接。我没有编写这段脚本,它按原样工作,但是当我尝试使用我在网上找到的任何方法时,它们要么破坏代码,要么只返回网站的 url 而不是外部 url

我尝试使用 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>

I'd like the urls that are external to open in a new tab and when the site radio button is selected return target=_self

【问题讨论】:

  • 这可能是不随机复制您在互联网上找到的代码的一个很好的理由。 window.open 是一个函数,而不是一个属性。你必须这样称呼它。你不能只为 window.open 赋值。
  • 我不得不从另一个库复制它,因为他们使用相同的代码,但他们没有在新标签中打开它,而这个库希望它在新标签中打开。

标签: javascript jquery


【解决方案1】:

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

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

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

【讨论】:

  • 请不要使用 W3Schools 作为参考。他们的文章经常过时,有时甚至完全是错误的。 MDN 是更可靠的资源:@​​987654322@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多