2010-03-29 64 views
0

我有一个下拉框和一些文本输入框。根据用户选择的下拉菜单中的哪个项目,我想禁用一些字段。我想我无法正确目标的输入字段,但我想不出是什么问题:基于从下拉列表中选择来禁用输入字段

这是迄今为止我已经得到了脚本:

$(document).ready(function(){ 
var customfield = $('#customfields-tf-19-tf'); 
var customfield1 = $('#customfields-tf-20-tf'); 
var customfield2 = $('#customfields-tf-13-tf'); 


$(function() { 
    var call_table = { 
    'Condominium': function() { 
    customfield.attr("disabled");  
    }, 
    'Co-Op': function() { 
    customfield1.attr("disabled"); 
    }, 
    'Condop': function() { 
    customfield2.attr("disabled"); 
    } 
    }; 

    $('#customfields-s-18-s').change(function() { 
    call_table[this.value](); 
    }); 

}); 

}); 

而对于我的表格布局:

<td width="260" class="left"> 
<label for="customfields-s-18-s">Ownership (Required):</label> 
</td> 
<td class="right"> 
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" size="" > 
<option value="Condominium"> Condominium</option> 
<option value="Co-Op"> Co-Op</option> 
<option value="Condop"> Condop</option> 
</select> 
</td> 
</tr> 



<tr> 
<td width="260" class="left"> 


    <label for="customfields-tf-19-tf">Maintenance:</label> 
</td> 
<td class="right"> 
    <input type="text" title="Maintenance" class="textInput" name="customfields-tf-19-tf" id="customfields-tf-19-tf" size="40"/> 
</td> 

</tr> 


<tr id="newsletter_topics"> 
<td width="260" class="left"> 

    <label for="customfields-tf-20-tf">Taxes:</label> 
</td> 
<td class="right"> 
    <input type="text" title="Taxes" class="textInput" name="customfields-tf-20-tf" id="customfields-tf-20-tf" size="40" /> 
</td> 
</tr> 


<tr> 
<td width="260" class="left"> 

    <label for="customfields-tf-13-tf" class="required">Tax Deductibility:</label> 
    </td> 
<td class="right"> 
    <input type="text" title="Tax Deductibility" class="textInput" name="customfields-tf-13-tf" id="customfields-tf-13-tf" size="40" /> 
</td> 
</tr> 
+0

建议你添加标签“jQuery”。 – 2010-03-29 03:11:46

回答

1

我认为,为了禁止输入你需要使用:

$(input).attr("disabled", "disabled"); 

而且,其中的一些功能可能会为你证明是有用的:

$.fn.enable = function() { 
    return this.removeAttr('disabled'); 
} 

$.fn.disable = function() { 
    return this.attr('disabled', 'disabled'); 
} 

$.fn.disenable = function(val) { 
    if (val) 
    return this.enable(); 
    else 
    return this.disable() 
} 

$.fn.toggleEnabled = function() { 
    if (this.attr('disabled') == 'disabled') 
    return this.enable(); 
    else 
    return this.disable(); 
} 

一旦被定义,然后你可以用它们在任何jQuery的变量,就像这样:

$(input).toggleEnabled(); 
+0

太棒了,就是我正在寻找的 - 而这些其他功能完全回答我的下一个问题,谢谢! – Thomas 2010-03-29 03:27:13

相关问题