简体   繁体   English

如何在wordpress网站中使用javascript重定向到另一个链接?

[英]How to redirect to another link with javascript in a wordpress site?

I have a wordpress geo theme driven website where user can search for events. 我有一个wordpress地理主题驱动的网站,用户可以在其中搜索事件。 When I start to write anything in the text box for searching event, then a auto completer suggestion comes in (which is a plugin). 当我开始在搜索事件的文本框中输入任何内容时,就会出现自动完成程序建议(这是一个插件)。 After clicking any suggestion it fills the textbox, and submits a form to index page. 单击任何建议后,它将填充文本框,并向索引页面提交表单。

But I need, after clicking the suggestion it will fill the text box(which is currently OK) and redirect the page to another link to same site like this: http://sitename.com/events/text-comes-from-text-box . 但是我需要,在单击建议后,它将填充文本框(当前确定),然后将页面重定向到另一个指向相同站点的链接,如下所示: http://sitename.com/events/text-comes-from-text-box : http://sitename.com/events/text-comes-from-text-box
"text-comes-from-text-box" is the text which will come form the text box after clicking the suggestion. “文本来自文本框”是单击建议后将从文本框中出来的文本。

Suggestions come from a plugin and suggestions come to this page "review_searchform.php". 建议来自插件,建议来自此页面“ review_searchform.php”。 Suggestions come as <div id='ac_results'><ul><li> . 建议来自<div id='ac_results'><ul><li> I am trying to find out,if I click on the <li> which is coming from the plugins is responding. 我正在尝试找出,如果我单击插件发出的<li>响应。 For do that I used this code in review_searchform.php this page, but it's not working. 为此,我在此页面的review_searchform.php使用了此代码,但是它不起作用。 My code is below, 我的代码如下

<script type="text/javascript">

    jQuery("#ac_results ul li").click(function() {
    alert("test 2");

});

If it would work, then I would add redirect code here. 如果可行,那么我将在此处添加重定向代码。 I think to solve this problem, it does not need PHP or WordPress functions. 我认为可以解决此问题,它不需要PHP或WordPress函数。 It only needs Javascript/jQuery. 它只需要Javascript / jQuery。

window.location = your_url将使您的浏览器立即重定向

Ok lets say your suggestion box is a div with class="suggestion" . 好吧,可以说您的建议框是一个带有class="suggestion"的div。

We want to redirect our page to this suggestions text as address. 我们想将页面重定向到此建议文本作为地址。

Example of your suggestion html 您的建议html的示例

<div class="suggestion">Air Control Service</div>

After clicking this link your visitor must go to this address right? 单击此链接后,您的访客必须转到该地址,对吗? :

http://sitename.com/events/Air-Control-Service

Ok then lets write our jquery code. 确定,然后编写我们的jquery代码。

$(document).ready(function(){ //Take care to conflict issue
  $(".suggestion").on("click", function(){ //When visitor clicks your suggestion
    var eventText = $(this).text(); //Get text from suggestion 
    eventText = eventText.replace(" ","-"); //Replace "-" for " "
    var base = "http://sitename.com/events/"; //Set your base address
    window.location.href = base + eventText; //Redirect your visitor
  });
});

That is all, I hope this will be helpfull. 仅此而已,希望对您有所帮助。

Edit: Changed my script to .on() version. 编辑:将我的脚本更改为.on()版本。 Now ajax returns also included in events. 现在,ajax返回也包含在事件中。

$("#ac_results ul li").each(function () {
    $(this).click(function () {
        window.location = $(this).text();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM