简体   繁体   English

如何在选择其他下拉列表中启用或禁用下拉列表或文本框?

[英]How to enable or disable drop-down or text-box on selection other drop-down?

<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. 我不知道我哪里出错了,所以应该做些什么让它发挥作用。

You're using a string ('true', 'false') instead of a boolean (true, false). 您使用的是字符串('true','false')而不是布尔值(true,false)。 Try this: 尝试这个:

<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>
....

Try: SAMPLE 尝试: 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') will return undefined . $(this).attr('value')将返回undefined You should use $(this).val() 你应该使用$(this).val()

You should use: 你应该使用:

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

Or this also should work: 或者这也应该有效:

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

In your code you are passing true and false as a string (first error) to the .attr() which is no longer used (second error) 在您的代码中,您将true和false作为字符串(第一个错误)传递给不再使用的.attr()(第二个错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM