简体   繁体   English

jQuery选择框交互

[英]jQuery select box interaction

I have got a working jsFiddle of a select drop-down box but it's not quite the result I need. 我有一个工作的jsFiddle的选择下拉框,但这不是我需要的结果。 For this example I have used 'Port of Auckland'. 在此示例中,我使用了“奥克兰港”。 If you click 'Port of Auckland' it will show it's content but I would like the drop-box value to change so it matches the port you're viewing. 如果您单击“奥克兰港”,它将显示其内容,但是我希望更改下拉框的值,以便它与您正在查看的港口相匹配。

UPDATE Solution was to use the prop function. 更新解决方案是使用prop函数。 Also, instead of duplicating all the ports... is there any easier way to minify the jQuery to work for all? 另外,不是复制所有端口...还有没有更简单的方法来缩小jQuery的适用范围? I then won't have to show/hide heaps of elements. 这样,我就不必显示/隐藏元素堆。

http://jsfiddle.net/an173g55/1/ http://jsfiddle.net/an173g55/1/

$(document).ready(function(){
    $("select").change(function(){
        $( "select option:selected").each(function(){
            if($(this).attr("value")=="all"){
                $(".auckland").hide();
                $(".all").show();   
            }
           if($(this).attr("value")=="auckland"){
                $(".all").hide();
                $(".auckland").show();

            }
        });
    }).change();

    $('.auckland-link').click(function() {
    $('.auckland').show();
    $(".all").hide();
});


});

Any help will be much appreciated. 任何帮助都感激不尽。 Cheers. 干杯。

You can change it using jQuery .prop() method. 您可以使用jQuery .prop()方法进行更改。

$(document).ready(function(){
    $("select").change(function(){
        $( "select option:selected").each(function(){
            if($(this).attr("value")=="all"){
                $(".auckland").hide();
                $(".all").show();



            }
           if($(this).attr("value")=="auckland"){
                $(".all").hide();
                $(".auckland").show();

            }
        });
    }).change();

    $('.auckland-link').click(function() {
        $('select option:contains("Auckland")').prop('selected', true);
        $('.auckland').show();
        $(".all").hide();
});


});

Here is a working sample of it, forked from your Fiddle: http://jsfiddle.net/Lny58ve3/2/ 这是它的工作示例,从您的Fiddle中派生出来: http : //jsfiddle.net/Lny58ve3/2/

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

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