【发布时间】: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