2016-05-12 3 views
0

Wie kann ich ein Attribut eines <option> in meinem zweiten <select> ändern, wenn der gleiche Wert in der ersten <select> ausgewählt wurde?Select2 Änderung attr in Sekunde wählen

Genauer gesagt: Wenn ich eine Option in meinem ersten <select> auswählen, sollte die gleiche Option in der zweiten <select> deaktiviert werden.

$("body").on("select2:select", "#first", function(e) { 
 
    var cur_id = e.params.data.id; 
 
    $("#second").children("option[value=" + cur_id + "]").attr("disabled", "disabled"); 
 
    $("#second").trigger('refresh'); 
 
    $("#first").trigger('refresh'); 
 
}); 
 

 
$("body").on("select2:select", "#second", function(e) { 
 
    var cur_id = e.params.data.id; 
 
    $("#first").children("option[value=" + cur_id + "]").attr("disabled", "disabled"); 
 
    $("#second").trigger('refresh'); 
 
    $("#first").trigger('refresh'); 
 
});
<body> 
 
    <select id="first" name="first"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
    
 
    <select id="second" name="second"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
</body>

Antwort

0

Für select2 Version> = 4.0.0

Lösung:

$("select").val("1").trigger("change"); 
+0

Nein, dieser Wert muss deaktiviert, aber nicht ausgewählt sein. – user4301988

0

$("body").on("change", "#first", function(e) {//use change event 
 
    var cur_id = $('option:selected',this).val();//get value of selected option 
 
    console.log(cur_id); 
 
    $(this).siblings("#second").children("option[value=" + cur_id + "]").attr("disabled", "disabled");//disable from sibling the selected option 
 
}); 
 

 
$("body").on("change", "#second", function(e) {//use change event 
 
    var cur_id = $('option:selected',this).val();//get value of selected option 
 
    console.log(cur_id); 
 
    $(this).siblings("#first").children("option[value=" + cur_id + "]").attr("disabled", "disabled");//disable from sibling the selected option 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
    <select id="first" name="first"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
    
 
    <select id="second" name="second"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
</body>

1

Hier ist ein Consise Weg, es zu tun.

$("select#first").on("change", function() { 
 
    $("select#second option").each(function() { 
 
    if ($("select#first option:selected").val() === $(this).val()) $(this).prop("disabled", true); 
 
    else $(this).prop("disabled", false); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
    <select id="first" name="first"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
    
 
    <select id="second" name="second"> 
 
    <option value="1" disabled="disabled">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    </select> 
 
</body>

Vielleicht möchten Sie auch eine andere Option automatisch auswählen (Kick) in Ihrem 2. wählen, wenn ein Benutzer die Option auswählt, die in der 2. select ausgewählt ist. Wenn das Sinn macht :-)

Verwandte Themen