How can I disable the select feature using javascript?

In the following HTML code, I would like to implement the following with JavaScript. When the user selects Audi

, he can only select application 2

( application 1

should disappear in this case).

<select>
    <option value="volvo">Volvo</option>
    <option value="audi">Audi</option>
</select>
<select>
    <option value="app1">application 1</option>
    <option value="app2">application 2</option>
</select>

      

I wrote the code here .

+3


source to share


4 answers


HTML:

 <select>
  <option id="app1" value="volvo">Volvo</option>
  <option id="app2" value="audi">Audi</option>
  </select>
 <select>
  <option value="app1">application 1</option>
  <option value="app2">application 2</option>
</select>

      

JS:



var selections = document.getElementsByTagName('select');
selections[0].onchange = carSelection;

function carSelection(e) {
    var first = selections[0];
    var id = first.options[first.selectedIndex].id;

    var second = selections[1];
    var  j = 0;
    while (second.options[j])  {
        second.options[j].disabled = (second.options[j].value != id );
        if (second.options[j].value == id)
            second.selectedIndex = j;
        j++;
    }    
}

      

http://jsfiddle.net/U4y2J/3/

+3


source


HTML:

 <select id="1">
  <option value="volvo">Volvo</option>
  <option value="audi">Audi</option>
  </select>
 <select id="2">
  <option id="2a" value="app1">application 1</option>
  <option id="2b" value="app2">application 2</option>
</select>

      

JavaScript:



<script type="text/javascript">
document.getElementById("1").onchange = change;
function change(){
    var s_a = document.getElementById("1");
    var car = s_a.options[s_a.selectedIndex].value;
    var s_b = document.getElementById("2");
    if(car === "audi"){
        s_b.options["2b"].disabled = true ;
        s_b.options["2a"].selected = true;
    }
    else {
        s_b.options["2b"].disabled = false ;
    }
}
</script>

      

And a link to the violinist

+1


source


Automatically deselect application 1

when selected Audi

.

http://jsfiddle.net/KQMLQ/1/

Html

 <select id="1">
  <option value="volvo">Volvo</option>
  <option value="audi">Audi</option>
  </select>
 <select id="2">
  <option value="app1">application 1</option>
  <option value="app2">application 2</option>
</select>

      

JQuery

$("#1").click(function() {
if($("#1").val() === 'volvo') {
    $("#2").find('option[value="app1"]').removeAttr('disabled');
}
if($("#1").val() === 'audi') {
    $("#2").find('option[value="app1"]').attr('disabled', 'disabled').removeAttr('selected');
    $("#2").find('option[value="app2"]').removeAttr('disabled');
}
});

      

+1


source


Here is a jQuery DEMO version
where I remove this parameter when select is audi - it is removed based on values, so you can have as many options that you want to surround with an option to remove

var opts=[];
$(function() {
    $("#sel2 option").each(function(){ // save all options
        var opt = {};
        opt.value=this.value;
        opt.text=this.text;
        opts.push(opt);
    });

    $("#sel1").on("change",function() { // restore relevant options
        var sel = $("#sel2");
        sel.empty();
        var val = $(this).val();
        $.each(opts,function(i,opt) {
          if (val == "audi" && opt.value=="app2") return;
          sel.append('<option value="'+opt.value+'">'+opt.text+'</option>');
        });
    });
});

      

+1


source







All Articles