2013-03-08 78 views
0
<script> 
$('#Select_Font').attr('disabled', 'true'); 
$('#Enter_Text').attr('disabled', 'true'); 
$('.dropdownstyle').change(function(){ 
    $(".dropdownstyle option:selected").text(); 
    if($(this).attr('value').match(/Custom$/)) 
    { 
     $('#Select_Font').attr('disabled', 'false'); 
     $('#Enter_Text').attr('disabled', 'false'); 
    } else if($(this).attr('value').match(/no Thanks$/)) 
    { 
     $('#Select_Font').attr('disabled', 'true'); 
     $('#Enter_Text').attr('disabled', 'true'); 
    } 
}); 
</script> 


<table> 
<tbody> 
<tr> 
<td> 
<span>Select type</span> 
</td> 
<td> 
<select class="dropdownstyle"> 
    <option value="No Thanks">No Thanks</option> 
    <option value="Custom lid (200)">Custom lid (200)</option> 
</select> 
</td> 
</tr> 
<tr> 
<td> 
<span>Select font</span> 
</td> 
<td> 
<select id="Select_Font"> 
    <option value="Arial">Arial</option> 
    <option value="georgia">georgia</option> 
</select> 
</td> 
</tr> 
<tr> 
<td> 
<span>enter text</span> 
</td> 
<td> 
<input type="text" id="Enter_Text"> 
</td> 
</tr> 
</tbody> 
</table> 

在上面的代码最初我希望字体下拉和文本框禁用,但选择自定义窗体第一个下拉我希望字体下拉和文本框启用。 我不确定我要出错的地方,所以应该做些什么才能使它工作。如何启用或禁用选择其他下拉菜单上的下拉菜单或文本框?

回答

1

尝试:SAMPLE

$('#Select_Font, #Enter_Text').attr('disabled', 'disabled'); 
$('.dropdownstyle').change(function() { 
    var present = $(this).val().match(/\Custom\b/gi) == null ? true : false; 
    if(present){ 
     $('#Select_Font, #Enter_Text').prop('disabled', 'disabled'); 
    } 
    else{ 
     $('#Select_Font, #Enter_Text').removeAttr('disabled'); 
    } 
}); 

$(this).attr('value')将返回undefined。你应该使用$(this).val()

+0

这是正确的$(this).attr('value')。match(/ Custom $ /)或者其他方式是更好的检查? – 2013-03-08 19:07:58

+0

查看更新的答案与工作小提琴... – Anujith 2013-03-08 19:09:09

+0

其工作谢谢... – 2013-03-08 19:18:20

0

你应该使用:

$("#id").prop("disabled", true); 

或者,这也应该工作:

$("#id")[0].disabled = true; 

在你的代码正在传递真假作为字符串(第一误差)的.attr() (第二个错误)

1

您正在使用字符串('true','false')而不是布尔值(true,false)。试试这个:

<script> 
$('#Select_Font').attr('disabled', true); 
$('#Enter_Text').attr('disabled', true); 
$('.dropdownstyle').change(function(){ 
    $(".dropdownstyle option:selected").text(); 
    if($(this).attr('value').match(/Custom$/)) 
    { 
     $('#Select_Font').attr('disabled', false); 
     $('#Enter_Text').attr('disabled', false); 
    } else if($(this).attr('value').match(/no Thanks$/)) 
    { 
     $('#Select_Font').attr('disabled', true); 
     $('#Enter_Text').attr('disabled', true); 
    } 
}); 
</script> 
.... 
+0

这是正确的'$(this).attr('value')。match(/ Custom $ /)'或者其他任何方式是更好的检查? – 2013-03-08 19:07:06