如何根据另一个</select>中的用户选择禁用<select>

时间:2014-01-03 08:25:09

标签: javascript jquery html

我有两个<select>,名为类别 category_sport

我希望<select name='category_sport'>停用,如果我点击<option>sport</option>中的<select name='category'>,我想启用<select name='category_sport'>

任何人都知道如何?

function disable()
    {
        document.getElementById("katOl").disabled=true;
    }
function enable()
    {
        document.getElementById("katOl").disabled=false;
    }

上面的脚本用于<option> ex:

<select name='category'>
  <option value="1" onclick='enable()' >sport</option>

</select>



<select id='katOl' name='category_sport'>
    <option value="1">Football</option>

 </select>

但不起作用

8 个答案:

答案 0 :(得分:2)

尝试这样的事情

的javascript

function check(val)
{
    if(val == '1'){
        document.getElementById("katOl").disabled=true;
    }else{
        document.getElementById("katOl").disabled=false;
    }
}

HTML

<select name='category' onchange="check(this.value)">
        <option value="" >SELECT</option>
        <option value="1" >sport</option>
</select>

答案 1 :(得分:0)

假设你看起来像这样:

<select name='category_sport' id="katOl"> ... </select>

HTML:

<select id="myselect" name='category' onchange="changeSelect()">
  <option value="1">sport</option>
</select>

使用Javascript:

function changeSelect(){
var mySelect = document.getElementById("myselect");
var selectedValue = e.options[myselect.selectedIndex].value;
if(selectedValue=="1") //sport
 {
   document.getElementById("katOl").disabled=false;
 }
}

答案 2 :(得分:0)

试试这个

$("#category").change(function(){
  if($(this).val()=="yourvalue"){
    disable();
  }else{
    enable();
  }
});

答案 3 :(得分:0)

你可以试试这个:

$(document).ready(function(){
  $('select[name=category]').change(function(){
    if($(this).val() === '1'){
      $('#katOl').prop('disabled', false);
    } else {
      $('#katOl').prop('disabled', true);
    }
  });
});

<强> DEMO Link

答案 4 :(得分:0)

使用select标签的onChange事件。

<select onChange="set_sport_cat(this)">
<option value="1">sport</option>
<option value="2">whatever</option>
</select>

function set_sport_cat(my){
    alert("test");
    if(my.options[0].selected){
        alert("1");
    }else{
        alert("2");
    }
}

http://jsfiddle.net/3A6X4/

答案 5 :(得分:0)

HTML CODE:

<select name='category' id="category">
  <option value="1">sport</option>
  <option value="2">education</option>
</select>
<select id='katOl' name='category_sport'>
  <option value="1">Football</option>
</select>

js代码:

(function(){
    var cat = document.getElementById('category'),
        katOl = document.getElementById('katOl');
    cat.onchange = function() {
      var v = cat.value;
      if(v === '1') {
          katOl.setAttribute('disabled', 'disabled');
      } else {
          katOl.removeAttribute('disabled');
      }
    }
})();

答案 6 :(得分:0)

这是我的解决方案:

HTML:

<select name="category" onchange="alterSelection(event)">
    <option value="">----</option>
    <option value="1">Sport</option>
    <option value="2">Food</option>
</select>

<select id="katOl" name="category_sport" disabled="disabled">
    <option value="1">Football</option>
    <option value="2">Cricket</option>
</select>

JS:

function alterSelection(e){
    if( e.target.selectedIndex==1 ){
        document.getElementById("katOl").disabled=false;
    }else{
        document.getElementById("katOl").disabled=true;
    }
}

答案 7 :(得分:0)

一种更简单有趣的方法:

LIVE DEMO

<select name='category'>
  <option>Sport</option>
  <option>Food</option>
  <option>Travel</option>
</select>

<select name='category_sport'>
  <option>Football</option>
</select>

<select name='category_food' disabled>
  <option>Pizza</option>
</select>

<select name='category_travel' disabled>
  <option>Croatia</option>
</select>

jQuery的:

$('select[name=category] option').click(function(){
  var val = this.value.toLowerCase(); // "Sport" >> "sport"
  $('select[name^=category_]').prop('disabled', 1); // Disable all
  $('select[name=category_'+ val +']').prop('disabled', 0); // Enable
});