简体   繁体   English

单击下拉列表时隐藏 label

[英]Hiding a label when drop-down list is clicked

I am displaying a label below a drop-down list once a task is complete, and I'd like the label to hide when a new task has been selected in the drop-down list without it posting back.一旦任务完成,我将在下拉列表下方显示 label,并且我希望 label 在下拉列表中选择新任务时隐藏而不发回。

I have tried using lblMessage.Text = "";我试过使用lblMessage.Text = ""; but currently have:但目前有:

<asp:DropDownList ID="ddlJob" runat="server" AutoPostBack="false" CssClass="combobox" 
  DataTextField="name" DataValueField="name" OnClick="hideOnKeyPress(); return true;">
</asp:DropDownList>
function hideOnKeyPress() {
    document.getElementById('lblMessage').style.display = 'none';
}

Expected: Label appears once task has finished and hides when a new one is selected.预期:Label 在任务完成后出现,并在选择新任务时隐藏。

Actual: Label appears once task has finished but remains until next selected task has finished.实际:Label 一旦任务完成就会出现,但会一直保持到下一个选定任务完成。

I think you need to access the OnChangeEvent, so when you change the option of the dropdown the function will be called.我认为您需要访问 OnChangeEvent,因此当您更改下拉选项时,将调用 function。

<asp:DropDownList Id="ddlJob" runat="server" AutoPostBack="false" OnSelectedIndexChanged="SelectedChange" 
      onchange="ddlChanged(this);"/>

In the function, if you store the current index value into a hidden field, you will have it even after a postback on the page.在 function 中,如果将当前索引值存储到隐藏字段中,即使在页面上回发后也将拥有它。 So if you have any postbacks on the page, you will still have that value.因此,如果您在页面上有任何回发,您仍将拥有该值。 In retrospect, if you have multiple different values you need to check if they have appeared before or not you can make a concatenated string with previous values to compare.回想起来,如果您有多个不同的值,您需要检查它们之前是否出现过,您可以将一个连接字符串与以前的值进行比较。

Something like:就像是:

function ddlChanged(ddl){
var newIndex = true;
var previousIndices = $('hdnIndex').value;
var currentIndex = String(ddl.selectedValue);

//if no values exist before then just take the current index
if (previousIndices == null)
   previousIndices = (currentIndex);
else{
//else loop through the previous indices to check if the value has appeared before.
 var indexArray = previousIndices.split(",");
 for(i=0; i < indexArray.length; i++){
   if (indexArray[i] == currentIndex)
     newIndex = false;
  }
  if(newIndex){
   document.getElementById('lblMessage').style.display = 'none';
    previousIndices+= "," + currentIndex;
  }
}

Something like this?像这样的东西?

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

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