使用jQuery过滤下拉菜单 [英] Filtering dropdown menus using jQuery
问题描述
由于某些原因,一些选择工作,但大多数不工作。我不知道我做错了什么,也许你可以帮助?
下面是关于codepen的现场演示: http://codepen.io/AlexBezuska/pen/dHmge
我的jQuery:
For some reason, some of the selections work but most don't. I can't figure out what I am doing wrong, maybe you can help? Here is a live demo on codepen: http://codepen.io/AlexBezuska/pen/dHmge My jQuery:
$('#ddlOffice option').hide();
$('#ddlBusiness').change(function(){
var selectedBusiness = $( "#ddlBusiness option:selected").val();
selectedBusiness = parseFloat(selectedBusiness);
$('#ddlOffice option').hide();
$('#ddlOffice option[value="'+selectedBusiness+'"]').show();
$('#txtBusiness').val($( "#ddlBusiness option:selected").text());
});
$('#ddlOffice').change(function(){
$('#txtOffice').val($( "#ddlOffice option:selected").text());
});
奖金积分:我想在没有jQuery的情况下这样做,如果任何人有一个简单的方法显示和隐藏在原始javascript或想要做一个叉子没有jQuery将是真棒,
Bonus points: I would like to do this without jQuery, if anyone has tips on an easy way of doing the showing and hiding in raw javascript or wants to do a fork without jQuery would be awesome,
谢谢!
推荐答案
我想出了一个更好的方法:
I figured out a better way:
var options = $("#ddlOffice").html();
$("#ddlBusiness").change(function(e) {
var selectedValue = $("#ddlBusiness :selected").val();
$("#ddlOffice").html(options);
$('#ddlOffice :not([value="'+selectedValue+'"])').remove();
});
现在正在工作CodePen: http://codepen.io/AlexBezuska/pen/dHmge
Now Working CodePen: http://codepen.io/AlexBezuska/pen/dHmge
这篇关于使用jQuery过滤下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!