组合 - 选择启用/禁用

时间:2012-11-19 17:13:22

标签: javascript jquery combobox

我有3个组合框(选择),如果我从第一个选项中选择一个选项,我需要启用第三个。

  • 类别(启用)
  • 子类别(禁用)
  • 葡萄(禁用)

如果我从Wines选择categories,我需要启用grapes

$('#subcategories').attr('disabled','disabled');
$('#grapes').attr('disabled','disabled');

$("#categories").change(function () {
$("#categories option:selected").each(function () {
    var v_elegido=$(this).val();    
    $('#subcategories').removeAttr('disabled');

    if($(this).val() == "Vinos")  {
        $('#grapes').removeAttr('disabled');
    } else {
        $('#grapes').attr('disabled','disabled');
    }

    $.post("subcategories.php", { elegido: v_elegido }, function(data){
        $("#subcategories").html(data);
    });  


});




 <?php if ($campo == 'varietales') { ?>    

    <div class="col_texto" align="right"><?php echo $campo ?>:&nbsp;</div>
  <div id="varietal" style="visibility:visible;">
        <select name="varietales" id="varietales">
        <option value="0">-</option>
        <?php
            $conn = DataBase::getInstance();
                $rel=false;
                $relacion='_'.$campo;
                $conn->setQuery('SELECT * FROM `_'.$campo.'` ORDER BY id ASC');
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo;
                    $conn->setQuery('SELECT * FROM `'.$campo.'` ORDER BY id ASC');
                }
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo.'s';
                    $conn->setQuery('SELECT * FROM `'.$campo.'s` ORDER BY id ASC');
                }
                if ($conn->len()>0){
                $rs = $conn->loadObjectList();
                foreach($rs as $valor){?>
            <option value="<?php echo $valor->id ?>"><?php echo $valor->titulo ?></option>
            <?php 
                }}?>
            </select> 
      </div>      
         <?php } else { ?>
    <?php if ($campo == 'bodegas') { ?>    

    <div class="col_texto" align="right"  style="visibility:visible;"><?php echo $campo ?>:&nbsp;</div>
  <div>
        <select name="bodegas" id="bodegas">
        <option value="0">-</option>
        <?php
            $conn = DataBase::getInstance();
                $rel=false;
                $relacion='_'.$campo;
                $conn->setQuery('SELECT * FROM `_'.$campo.'` ORDER BY id ASC');
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo;
                    $conn->setQuery('SELECT * FROM `'.$campo.'` ORDER BY id ASC');
                }
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo.'s';
                    $conn->setQuery('SELECT * FROM `'.$campo.'s` ORDER BY id ASC');
                }
                if ($conn->len()>0){
                $rs = $conn->loadObjectList();
                foreach($rs as $valor){?>
            <option value="<?php echo $valor->id ?>"><?php echo $valor->titulo ?></option>
            <?php 
                }}?>
            </select> 
      </div>      
         <?php } else { ?>

   <?php if ($campo == 'subcategorias') { ?>    

    <div class="col_texto" align="right"><?php echo $campo ?>:&nbsp;</div>
  <div>
        <select name="<?php echo $campo ?>" id="<?php echo $campo ?>">
        <option value="0">-</option>
        <?php
            $conn = DataBase::getInstance();
                $rel=false;
                $relacion='_'.$campo;
                $conn->setQuery('SELECT * FROM `_'.$campo.'` ORDER BY id ASC');
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo;
                    $conn->setQuery('SELECT * FROM `'.$campo.'` ORDER BY id ASC');
                }
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo.'s';
                    $conn->setQuery('SELECT * FROM `'.$campo.'s` ORDER BY id ASC');
                }
                if ($conn->len()>0){
                $rs = $conn->loadObjectList();
                foreach($rs as $valor){?>
            <option value="<?php echo $valor->id ?>"><?php echo $valor->titulo ?></option>
            <?php 
                }}?>
            </select>
    </div>        


         <?php  } else { ?> 



         <div class="col_texto" align="right"><?php echo $campo ?>:&nbsp;</div>
         <div>
        <select name="categorias" id="categorias">
        <option value="0">-</option>
        <?php
            $conn = DataBase::getInstance();
                $rel=false;
                $relacion='_'.$campo;
                $conn->setQuery('SELECT * FROM `_'.$campo.'` ORDER BY id ASC');
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo;
                    $conn->setQuery('SELECT * FROM `'.$campo.'` ORDER BY id ASC');
                }
                if ($conn->len()==0){
                    $rel=true;
                    $relacion=$campo.'s';
                    $conn->setQuery('SELECT * FROM `'.$campo.'s` ORDER BY id ASC');
                }
                if ($conn->len()>0){
                $rs = $conn->loadObjectList();
                foreach($rs as $valor){?>
            <option value="<?php echo $valor->id ?>"><?php echo $valor->titulo ?></option>
            <?php 
                }}?>
            </select>
           </div>  

         <?php } ?>      
         <?php } ?>          
         <?php } ?>  

1 个答案:

答案 0 :(得分:0)

所以你需要做的就是获取val ..并相应地设置disabled属性

$("#categories").change(function(){
    $('#grapes').prop('disabled',$(this).val() != 'Vinos');
});

这假设您有选项的值,第一个选择的第三个选项是'Vinos'

.prop()也是自jQuery 1.6 +

以来设置disabled属性的首选方法
  

应该使用.prop()方法设置disabled和checked而不是.attr()方法