如何在选择其他下拉菜单时启用或禁用下拉菜单或文本框?
问题描述:
<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>
在上面的代码中,最初我希望禁用字体下拉菜单和文本框,但是在选择自定义窗体的第一个下拉菜单时,我希望启用字体下拉菜单和文本框. 我不确定哪里出了问题,应该怎么做才能使其正常工作.
In above code initially I want font drop-down and text-box disable but on select of custom form first drop-down I want font drop-down and text box to be enable. I am not sure where I am going wrong, so what should be done to make it work.
答
尝试: 示例
Try: 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()
$(this).attr('value')
will return undefined
. You should use $(this).val()