简体   繁体   English

如果文本框有价值,则显示下拉菜单

[英]Show drop down if text box has value

I need to show drop down only if any value is provided in text box, other wise the drop down should be hidden. 仅在文本框中提供任何值时,我才需要显示下拉菜单,否则应该隐藏下拉菜单。 I tried the following code with javascript but it is not working. 我用javascript尝试了以下代码,但无法正常工作。 With this code, the drop down is not hidden when there no value is typed in text box field. 使用此代码,当在文本框字段中未键入任何值时,下拉菜单不会隐藏。 It is showing all the time. 它一直在显示。

<input type="text" name="example_textbox" id="textboxId" onclick="javascript:onTextboxChange()" >

<select name="example_dropdown" id="dropdownId" >
   <option selected value="0">Select One</option>
   <option value="Option1">Option1</option>
   <option value="Option2">Option2</option>
   <option value="Option3">Option3</option>
      </select>

<script type="text/javascript">
var getTextBox = document.getElementById('textboxId');
var getDropDown = document.getElementById('dropdownId');

function onTextboxChange(){
    if (getTextBox.value != null)
        {
        getDropDown.disable='false';
        //getDropDown.style.display = 'inline';
        }
    else{
        //getDropDown.style.display = 'none';
        getDropDown.disable='false';
    }
}

Any suggestion what should I do to make it work? 有什么建议我应该怎么做才能使其起作用?

For starters, you don't need the "javascript" part in the handler: 对于初学者,您不需要处理程序中的“ javascript”部分:

<input type="text" name="example_textbox" id="textboxId" onclick="onTextboxChange()" >

You probably also want to check against an empty string and not just null as well: 您可能还想检查一个空字符串,而不仅仅是null

if (getTextBox.value != null && getTextBox.value != '')

I'm not sure about a disable property (not to mention both conditions set it to the same value), but your styling changes work: 我不确定disable属性(更不用说两个条件都将其设置为相同的值),但是您的样式更改有效:

if (getTextBox.value != null && getTextBox.value != '') {
    getDropDown.style.display = 'inline';
} else {
    getDropDown.style.display = 'none';
}

With those changes in place, it seems to be working . 完成这些更改后,它似乎正在工作 Though it's strange that this is happening on a click event in an input . 尽管很奇怪,这是在inputclick事件中发生的。 Maybe you meant the change event? 也许您是说变更事件? Or even the keyup event? 甚至是按键事件?

You need to bind the function with the oninput event of the input-box 您需要将函数与输入框的oninput事件绑定

Also you need to hide the dropdown initially on the page-load. 另外,您还需要在页面加载时最初隐藏下拉菜单。

 var getTextBox = document.getElementById('textboxId'); var getDropDown = document.getElementById('dropdownId'); onTextboxChange(); function onTextboxChange() { if (getTextBox.value != null && getTextBox.value != '') { getDropDown.style.display = 'inline-block'; } else { getDropDown.style.display = 'none'; } } 
 #textboxId { display:inline-block; position:absolute; top:0px; left:10px; } select { display:inline-block; position:absolute; top:0px; left:250px; } 
 <input type="text" name="example_textbox" id="textboxId" oninput="onTextboxChange()"> <select name="example_dropdown" id="dropdownId"> <option selected value="0">Select One</option> <option value="Option1">Option1</option> <option value="Option2">Option2</option> <option value="Option3">Option3</option> </select> 

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

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