2013-03-08 104 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