简体   繁体   English

jQuery-有没有更有效的方法来做到这一点?

[英]jQuery - Is there a more efficient way to do this?

http://jsfiddle.net/bGDME/ http://jsfiddle.net/bGDME/

Basically, I want to show only whatever is selected in the scope and hide the rest. 基本上,我只想显示范围中选择的任何内容,然后隐藏其余的内容。

The way I did it seems so.. I don't know. 我的方式似乎是这样。.我不知道。 Tedious. 乏味。

I was hoping to get some ideas of making it better. 我希望能得到一些更好的想法。 A point in the right direction would be very much appreciated, too. 朝着正确方向的观点也将不胜感激。

Thanks. 谢谢。

You can minimize the code by using toggle() instead of your if/else statements 您可以通过使用toggle()而不是if / else语句来最小化代码

Working Example: http://jsfiddle.net/hunter/bGDME/1/ 工作示例: http : //jsfiddle.net/hunter/bGDME/1/

$('#scope').change( function(){
    var type = $('option:selected', this).val();
    $('#grade').toggle(type == 2 || type == 3);
    $('#class').toggle(type == 3);
});

.toggle(showOrHide) .toggle(showOrHide)

showOrHide: A Boolean indicating whether to show or hide the elements. showOrHide:一个布尔值,指示是否显示或隐藏元素。

what about this?? 那这个呢??

  $(document).ready( function() {    
    $('#grade, #class').hide();    
    $('#scope').change( function(){    
        var type = $('option:selected', this).text();
        alert(type);      
        $('select').next().not('#'+type).hide();
        $('#'+type).show();       
    });
});

DEMO DEMO

This seems fine to me, unless you have lots of or dynamic controls. 对我来说,这似乎很好,除非您有很多控件或动态控件。 However u can use JQuery addClass / removeClass , switch statement, multiple Selector $('#grade, #class').show(); 但是你可以使用JQuery addClass / removeClassswitch语句,多个Selector $('#grade, #class').show(); to minimize the code 最小化代码

您还可以使用切换状态: http : //jsfiddle.net/bGDME/3/

This will do what you're looking for: http://jsfiddle.net/bGDME/30/ 这将满足您的需求: http : //jsfiddle.net/bGDME/30/

You simply use the val() of the scope within the eq() method to determine which sibling select should remain shown. 只需使用val()的的scope的内eq()方法来确定哪个同级select应保持所示。 If 'school' is chosen from the first dropdown, then neither get shown: 如果从第一个下拉菜单中选择了“学校”,则都不会显示:

$(document).ready( function() {

    var additionalSelects = $('#grade, #class');

    $('#scope').change(function(){
        var selectedVal = $(this).val();
        additionalSelects.hide();
        if(selectedVal > 1){
            additionalSelects.eq(selectedVal - 2).show();
        }
    });
});

Here's an approach using HTML5 data attributes to declaratively set "scope levels" on the select boxes: http://jsfiddle.net/bGDME/6/ 这是一种使用HTML5数据属性在选择框上声明性地设置“范围级别”的方法: http : //jsfiddle.net/bGDME/6/

And the updated JavaScript: 以及更新的JavaScript:

var $scopedSelects = $('#grade, #class').hide();
$('#scope').change( function(){
    var scopeLevel = $(this).val();
    $scopedSelects.each(function() {
        var $this = $(this);
        $this[$this.data('scope-level') <= scopeLevel ? 'show' : 'hide']();
    });
});

The primary advantage this one might have is that the code stays the same regardless of how many "scoped selects" you have (assuming you update the initial selector, of course). 该代码可能具有的主要优点是,无论您有多少“范围选择”,代码都保持不变(当然,假设您更新了初始选择器)。

Its very simple, 非常简单

$(document).ready( function() {
    $("select[id!='scope'][id!='school']").hide();
    $('#scope').change( function(){
        $("select[id!='scope']").hide();
        var ken=$(this).val();
        $("#"+ken).show();
    });
});

If you want to make it a bit more dynamic by not touching the javascript when you want to add more select elements, then you can do small changes to your javascript code and HTML and you will only need to edit the HTML 如果要通过添加更多选择元素时不触摸javascript来使其更具动态性,则可以对javascript代码和HTML进行少量更改,而只需编辑HTML

Javascript: 使用Javascript:

$(document).ready(function() {
$('#scope').change(function() {
    var type = $(this).val().split(',');
    $('.values select').hide();
    for (x in type) {
        $('.values').find('#'+type[x]).show();
    }

});

}); });

HTML: HTML:

<select id='scope'>
<option value=''>Select</option>
<option value='school'>school</option>
<option value='school,grade'>grade</option>
<option value='school,grade,class'>class</option></select>

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

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