停止下拉菜单重定向到首页 [英] Stop dropdown from redirecting to homepage

查看:81
本文介绍了停止下拉菜单重定向到首页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从下拉菜单选项中删除以"+"字符开头的文本:

sI am using the following code to remove text starting from the "+" character from dropdown menu choices:

 $("select").each(function(){
  var $wrapper=$(this);
  var $options=$wrapper.find("option");
  $(this).empty();
  $options.each(function(index){
      $wrapper.append(new Option($(this).text().split("+")[0]))
  })
})

因此,如果菜单的原始选择是:

So if a menu original choices would be:

奥迪

宝马

奔驰+拖车

该下拉列表仅显示:

奥迪

宝马

奔驰

问题:

做出选择后,我还有一些下拉菜单需要刷新页面.如果不使用上述代码,则可以正常工作,但是在网页上方添加代码时,则会重定向到首页.如何确保用户做出下拉选择时用户停留在同一页面上?

I also have dropdowns that need to refresh the page when a choice is made. When not using the above code this works fine, but when adding the code above the webpage redirects to the homepage instead. How I can ensure that when the user makes a dropdown choice the user stays on the same page?

推荐答案

遵循jquery的精神,少写,多做",您也可以像这样进行文本替换操作:

Following the jquery spirit "write less, do more" you could also do the text replacement operation like this:

$("select.cars option").each(function(){
 with ($(this)) {
  text(text().split("+")[0].trim());
  val(val().split("+")[0].trim());
 }
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="cars">
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Mercedes + Trailer">Mercedes + Trailer</option>

</select>

为了限制脚本对您要更改的选择的影响并保护所有其他选择,我还在图片中添加了汽车"类.也许遵循这些原则对您的项目有帮助?

To limit the effect of the script on the selects you want to change and protect all the others I also added the class "cars" into the picture. Maybe something along those lines can be helpful for your project?

但这不会(不会)解决您当前遇到的问题,因为一旦选择了选择选项,您便会离开当前页面.我怀疑还必须有另一个脚本(尚未发布)来负责该行为.

But this will not touch your current problem, that you leave the current page as soon as a select option has been selected. I suspect there must be another piece of (as yet unpublished) script that is responsible for that behaviour.

这篇关于停止下拉菜单重定向到首页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆