2016-06-21 15 views
0

を持つすべての選択ボックスの値Iは、これらの選択ボックスで、この変更jqueryの

<select class="orderselect" name="files_Article_json[]"> 
    <option value="1" selected="">general</option> 
    <option value="2">one</option> 
</select> 
. 
. 
. 
<select class="orderselect" name="files_Article_json[]"> 
    <option value="1">general</option> 
    <option value="2" selected="">one</option> 
</select> 

のようないくつかの選択ボックスのHTMLを持って、私はマルチvalue="1"を選択することができますが、私は、私はこのjQueryを使ってそれを行う1 value="2"

を選択することができますコード:

$(document).on("change", ".orderselect",function() { 
    if($(this).val() == '2') { 
     $(".orderselect").val('1').prop('selected', true); // first set all select box value to 1 
     $(this).val('2').prop('selected', true); // set current selectbox to 2 
    } 
}); 

今は3つの価値があります。再び

<select class="orderselect" name="files_Article_json[]"> 
    <option value="1" selected="">general</option> 
    <option value="2">one</option> 
    <option value="3">two</option> 
</select> 

、私は一つだけvalue=2(1なっておきに、value=2と選択値のため)を選択することができるマルチvalue=1

を選択することができます。

value=3のいずれか1つのみを選択することができます(1つおきにvalue=3を選択すると値は1になります)。

私はそれを行う試してください。

if($(this).val() == '2') { 

    // for all orderselect with 2 value: 
    $(".orderselect").val('1').prop('selected', true); 

    $(this).val('2').prop('selected', true); 
} 

if($(this).val() == '3') { 

    // for all orderselect with 3 value: 
    $(".orderselect").val('1').prop('selected', true); 

    $(this).val('3').prop('selected', true); 
} 

答えて

1

私はこの小さな機能を働きました。私にとって本当にうまくいった:

<script> 
$(document).on("change", ".orderselect",function() { 
if($(this).val() != '1') { 
    var num = $(this).val(); 
    $.each($(".orderselect").not(this),function(){ 
     if($(this).val() == num){ 
      $(this).val('1').prop('selected', true); 
     } 
    }); 
} 
}); 
</script>