elize elize - 5 months ago 9
HTML Question

change all select box value with jquery

I have some selectbox html like this

<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>


in these selectbox, I can select multi
value="1"
but I can only select one
value="2"


I do it with this jquery code:

$(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
}
});


NOW I want do it for three value.

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


again,I can select multi
value=1


I can select only one
value=2
(for every other select with
value=2
,value become 1).

I can select only one
value=3
(for every other select
value=3
,value become 1).

I try do it:

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);
}

Answer

I worked this little function out. Worked real well for me:

<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>
Comments