Perfect solution in html can not select the option to hide the problem.

The development process is not intended to encounter this problem, to solve the problem the first time that Baidu. The results obtained as follows:

1, the first set option display: none is certainly not feasible solution;
2, a User proposed two solutions:
. A top with the option tag disabled = "disabled" attribute indicates not available, but this scheme so that option can not be selected, but did not hide
b. If you want to hide out, the option can only be removed from the DOM tree, and then remove the cache option, and then removed from the cache option is added at the time to be displayed in the DOM tree
and certainly does not meet the requirements.
c, the ultimate program (has been tested and compatible with all browsers): to option plus a parent tag, and set the parent tag hide [here use the span tag], if you want to display, then the parent label can then be removed.

However, the above did what so many ways

Last solution:

//将select通过clone方法保存
var select= $("#select").clone();

//删除所有的option
$("#select").find('option').remove();

//查找出需要显示的option并复制
var options = select.find("option[value=1]").clone();

//将需要显示的option添加到select中
$("#select").append(options);

//因为option.remove()不会刷新控件,需要将新的option切换上去
//这里排除了options.size() == 0的情况
$("#select").find('option').eq(0).attr("selected",true);

That's all.

Guess you like

Origin www.cnblogs.com/homehtml/p/12577629.html