根据同一表单中下拉菜单的值启用 HTML 表单文本框? [英] Enable an HTML form text box based on the value of a drop down menu in the same form?

查看:22
本文介绍了根据同一表单中下拉菜单的值启用 HTML 表单文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 HTML 的新手,我知道这个问题在这里已经被问过好几次了,但我已经尝试了所有不同的解决方案,但没有一个对我有用.当从下拉菜单中选择两个值之一时,我试图激活一个字段(默认为禁用).这是我为完成任务而编写的 JavaScript 函数.

I'm new to HTML and I'm aware that this question has been asked several times on here, but I've tried all the various solutions and none have worked for me. I'm trying to activate a field (which defaults to disabled) when one of two values is selected from a drop down menu. This is the JavaScript function I have written to accomplish the task.

function changeTextBox() {
    comp = document.getElementById('comparator');
    if(comp.value=='equalto'||comp.value=='notequalto')
        document.getElementById('localevalue').disable=false;
    else
        document.getElementById('localevalue').disable=true;    
}

这是下拉菜单

<label style="font-size:16px" for="comparator">Comparator:</label>
<select id="comparator" name="comparator" onChange="changeTextBox();">
    <option value="lessthan">Less Than</option>
    <option value="lessthanequalto">Less Than or Equal To</option>
    <option value="greaterthan">Greater Than</option>
    <option value="greaterthanequalto">Greater Than or Equal To</option>
    <option value="equalto">Equal To</option>
    <option value="notequalto">Not Equal To</option>
    <option value="exists">Exists</option>
</select>

这是在下拉菜单中选择等于"或不等于"时需要激活的字段.

And this is the field that needs to be activated when 'Equal To' or 'Not Equal To' are selected from the drop down menu.

<label style="font-size:16px" for="value">Value:</label>
<input type="number" id="value" disabled/>

但是当我选择其中一个字段时,该框仍处于停用状态.谁能告诉我我做错了什么?

But when I select one of the fields, the box remains deactivated. Can anyone tell me what I'm doing wrong?

推荐答案

问题是我应该在我的 JS 函数中使用 .disabled,而不是 .disable.

The problem was that I should have been using .disabled, not .disable in my JS function.

这篇关于根据同一表单中下拉菜单的值启用 HTML 表单文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆