简体   繁体   English

jQuery Select 盒子重定向

[英]jQuery Select Box Redirections

I'm having a bit of a mare with jQuery and I was hoping someone could clarify some bits for me.我对 jQuery 有点麻烦,我希望有人能为我澄清一些问题。 I'm under no illusion any of this is elegant or optimal, and I have NEVER got on with Javascript in any form so please don't judge me too harshly!我不认为这是优雅或最佳的,我从来没有以任何形式使用 Javascript,所以请不要太严厉地评判我!

Basically, I've got a Wordpress plugin that works very well for most things I want it to do.基本上,我有一个 Wordpress 插件,它非常适合我想要它做的大多数事情。 The main thing is that it offers the ability to select the delivery method from the Woocommerce product menu page.最主要的是它提供了 select 产品菜单页面中的交货方式的能力。 But I've been asked for the available menu options to be filtered by delivery method and I can't see a way of doing this live on the page, so what I've come up with is a "simpler" solution.但是我被要求按交付方式过滤可用的菜单选项,我看不到在页面上实时执行此操作的方法,所以我想出了一个“更简单”的解决方案。 I'm intending to set up three different page implementations of the menu, one for each of the delivery options (deliver, pick up and eat in), each with categories filtered via shortcode for that delivery method.我打算设置菜单的三个不同页面实现,一个用于每个交付选项(交付、取货和就餐),每个都有通过该交付方法的短代码过滤的类别。 When the page loads I've got jQuery setting the default option for the dropdown to match that page and then whenever the dropdown is changed from this option it can trigger a redirect to one of the other relevant pages.当页面加载时,我有 jQuery 设置下拉菜单的默认选项以匹配该页面,然后每当下拉菜单从此选项更改时,它都可以触发重定向到其他相关页面之一。

I know this is a messy way of doing it, but I have no idea how or even if I could filter the products on the menu any other way, but if someone has a suggestion I'd love to hear it!我知道这是一种混乱的做法,但我不知道如何或什至可以以任何其他方式过滤菜单上的产品,但如果有人有建议,我很想听听!

Anyway, I've got this partially working.无论如何,我已经部分工作了。 I can set the dropdown default on page load easily enough, and I've managed to get an if statement kind of working, but it only seems to work for the first option and no others.我可以很容易地在页面加载时设置下拉默认值,并且我已经设法使 if 语句起作用,但它似乎只适用于第一个选项而没有其他选项。 I also can briefly see ifelse undefined in the console before the page changes on a redirect.在重定向页面更改之前,我还可以在控制台中简要查看ifelse undefined I've also tried to use a variable, but whenever I've tried using it in the if condition checks it just comes up as undefined or null even though I can view it on the previous line like this:我也尝试过使用一个变量,但是每当我尝试在 if 条件检查中使用它时,它只会出现为 undefined 或 null 即使我可以在上一行中查看它,如下所示:

 jQuery("#fdoe_delivery_dropdown").on("change",function(){//Getting Value var selValue = $("#fdoe_delivery_dropdown").val(); console.log ("Variable delivery method is - " + selValue);

Obviously, I know a variable would be a better way to go, but because I can't get it to work (and I know it's me being dumb but I just gave up on it), here's what I've got so far:显然,我知道一个变量将是 go 的更好方法,但因为我无法让它工作(我知道这是我很愚蠢,但我只是放弃了它),这就是我到目前为止所得到的:

 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script> <script type="text/javascript"> jQuery(document).ready(function($) { //Set opening delivery method for "Delivery" page jQuery("#fdoe_delivery_dropdown").val("delivery").change(); //Check initial delivery method console.log ("Opening delivery method is - " + jQuery('#fdoe_delivery_dropdown').val()); if($("#fdoe_delivery_dropdown option:selected").val() == "pickup") { //Log success to console and redirect console.log ("New delivery method is - " + jQuery('#fdoe_delivery_dropdown').val()); window.location.replace("{site_url}/pickup/"); } elseif($("#fdoe_delivery_dropdown option:selected").val() == "eatin") { //Log success to console and redirect console.log ("New delivery method is - " + jQuery('#fdoe_delivery_dropdown').val()); window.location.replace("{site_url}/eatin/"); } }); }); </script>

HTML: HTML:

 <select id="fdoe_delivery_dropdown" class="form-control input-lg"><option value="pickup" selected="">Pick Up</option><option value="delivery">Delivery</option></select>

Note: I have used the WordPress variable {site_url} instead of publishing the website on here, I have no idea if this would actually work in the jQuery, but it would be much better to use a dynamic URL like this rather than a hard-coded one, so if anyone knows the best way of doing that as well it'd be great to know.注意:我使用了 WordPress 变量 {site_url} 而不是在此处发布网站,我不知道这是否真的适用于 jQuery,但使用动态 ZE6B391A8D2C4D458503A23A 会好得多编码一个,所以如果有人知道这样做的最佳方法,那就太好了。

As I've said, this script partially works and I've had to put it in a HTML block on the individual pages to get it to trigger at present.正如我所说,这个脚本部分工作,我不得不把它放在各个页面上的 HTML 块中,以便目前触发它。 So if anyone could help me tidy this up, point out the millions of ways I'm going wrong and help me get this working fully, it would be MASSIVELY appreciated!因此,如果有人可以帮我整理一下,指出我出错的数百万种方式并帮助我充分发挥作用,我们将不胜感激!

Thanks in advance!提前致谢!

I think you want to achieve redirect based on the select option.我认为您想基于 select 选项实现重定向。 check below code.检查下面的代码。

 $('#fdoe_delivery_dropdown').on('change', function(){ window.location = 'https://exmaple.com/'+$(this).val(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="fdoe_delivery_dropdown" class="form-control input-lg"> <option value="pickup" selected="">Pick Up</option> <option value="delivery">Delivery</option </select>

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

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