0

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

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

如果我选择Winescategories需要启用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 } ?>  
4

1 回答 1

0

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

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

这是假设您有选项的值,并且您首先选择的第三个选项是“Vinos”

.prop()也是自 jQuery 1.6+ 以来设置 disabled 属性的首选方法

应该使用 .prop() 方法来设置禁用和检查,而不是 .attr() 方法

于 2012-11-19T17:21:20.513 回答