简体   繁体   English

如何使用jquery更改所选菜单选项

[英]How to change selected menu option using jquery

Hi I am using jquery chosen plugin. 嗨我正在使用jquery选择的插件。 I want to selected menu value by selected value from other select menu. 我想从其他选择菜单中选择值来选择菜单值。 But my code working with simple selected menu. 但我的代码使用简单的选择菜单。 I want it to work with jquery chosen plugin. 我希望它与jquery选择的插件一起使用。 Example code 示例代码

$(".chzn-select").chosen()
    $('[id*=second]').change(function(){
    var val = $(this).find('option:selected').val()

    if(val.toLowerCase()=='mr'){
    $('[id*=gender]').find('option').eq(0).attr('selected', 'selected');
}

else{       
    $('[id*=gender]').find('option').eq(1).attr('selected', 'selected')
     }
})
​

You should add value to the options and try to match those value onchange . 您应该为选项添加value并尝试匹配onchange值。 See my implementation below and let me know if it works for you. 请参阅下面的实施,如果它适合您,请告诉我。

Edit: Added few lines to update the plugin selection. 编辑:添加了几行来更新插件选择。

DEMO: http://jsfiddle.net/vUkQg/3/ 演示: http //jsfiddle.net/vUkQg/3/

Note: Below method can relate any number of drop downs with just same class and value .(no change to the script) http://jsfiddle.net/vCE4Y/18/ 注意:下面的方法可以将任意数量的下拉列表与相同的类和value 。(脚本没有变化) http://jsfiddle.net/vCE4Y/18/

HTML: HTML:

<select id="second" class="chzn-select" style="width:100px">
  <option value="1"> mr</option>
  <option value="2"> mrs</option>
</select>

<select id="gender" class="chzn-select" style="width:100px">
  <option value="1"> male</option>
  <option value="2"> female</option>
</select>

JS: JS:

$(function() {
    $(".chzn-select").chosen().change(function() {
        var val = $(this).val();
        $('.chzn-select').val(val);

        $('.chzn-select').not(this).each(function () {                       
            var $chzn = $('#' + this.id + '_chzn');
            var $li = $chzn.find('li');            
            $li.removeClass('result-selected');  

            var selectedLi = $li.eq(val - 1);
            selectedLi.addClass('result-selected');

            $chzn.find('a.chzn-single span').html(selectedLi.html());            
            //update selected result in plugin
            var $data = $(this).data('chosen');

            $.each($data['results_data'], function (idx, el) {
                if (idx == val-1) {
                    el.selected = true;
                    $data.result_single_selected = selectedLi;
                } else {
                    el.selected = false;
                }
            });
        });
    });
});

Demo 演示

I hope the below script does what you expect 我希望下面的脚本符合您的期望

$(".chzn-select").chosen();
$('[id*=second]').change(function(){
    var val = $('div[id*=second]').find('li.result-selected').html().trim();
    $('select[id*=gender]').find('option').removeAttr('selected');
    $('div[id*=gender]').find('li.active-result').removeClass('result-selected');
    var selectedValue = "";
    if(val.toLowerCase()=='mr'){
        selectedValue = $('div[id*=gender]').find('li.active-result').eq(0).addClass('result-selected').html();
        $('select[id*=gender]').find('option').eq(0).attr('selected','selected');
    } else {
        selectedValue = $('div[id*=gender]').find('li.active-result').eq(1).addClass('result-selected').html();
        $('select[id*=gender]').find('option').eq(1).attr('selected','selected');
    }
    $('div[id*=gender]').find('a.chzn-single span').html(selectedValue);
});

Use selectedIndex 使用selectedIndex

eg: 例如:

   document.getElementById('selectId').selectedIndex = 0; 

   //selectId - id of the select elem

<select id="selectId">
  <option value="1" selected="selected">Male</option>
  <option value="2">Female</option>
</select>

To get the Selected value 获取Selected值

    var e = document.getElementById("selectId");
    var strUser = e.options[e.selectedIndex].value;   

here strUser = 1; 这里strUser = 1;

if you want the TEXT 如果你想要TEXT

    var strUser = e.options[e.selectedIndex].text;

here strUser = Male; 这里strUser =男;

i think this helps you. 我认为这有助于你。

<select id="second" class="chzn-select" style="width: 100px">
    <option value="1">mr</option>
    <option value="2">mrs</option>
</select>
<select id="gender" class="chzn-select" style="width: 100px">
    <option value="1">male</option>
    <option value="2">female</option>
</select>​

   $(document).ready(function () {
        $('.chzn-select').change(function () {
            $('.chzn-select').val($(this).val());
            //$('.chzn-select').val($(this).val()).trigger('change');
        });
    });​

Demo 演示

I used the value of the options, the minus 1 is to reach the indexnumber 我使用了选项的值,减1是达到索引号

DEMO DEMO

 $('#second').change(function(){
      var val = $(this).val();

    $('#gender option').eq(val - 1).attr('selected', 'selected');
});

or 要么

$('#second').change(function(){
    var index = $(this)[0].selectedIndex;

    $('#gender option').eq(index).attr('selected', 'selected');
});

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

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