问题
How can I have the first item in a listbox disabled? Following is my code:
ListBox list = new ListBox();
list.addItem("Select an item");
list.addItem("a");
list.addItem("b");
list.addItem("c");
How do I disable the first item in list? Thanks so much
回答1:
You could select the first child element and set the disabled atribute:
list.getElement().getFirstChildElement().setAttribute("disabled", "disabled");
回答2:
For anyone who finds this page and is looking to disable a GWT ListBox option which is NOT the first option in the ListBox, this worked for me:
list.getElement().getElementsByTagName("option").getItem(itemNumber).setAttribute("disabled", "disabled");
Where itemNumber is the option you would like to disable.
回答3:
list.getElement().<SelectElement>cast().getOptions().getItem(0).setDisabled(true);
回答4:
On six lines:
private void disableOption(ListBox listBox, int optionNumber, boolean disable){
if(optionNumber<=listBox.getElement().getChildCount()){
Element element = listBox.getElement().getFirstChildElement();
while (--optionNumber > 0){
element = element.getNextSiblingElement();
}
element.setAttribute("disabled", disable ? "true" : "false");
}
}
来源:https://stackoverflow.com/questions/6269878/gwt-listbox-how-to-have-a-listbox-item-disabled