jQuery select box with disable-able options
I'm looking for a jQuery/JavaScript select box for my HTML forms. The specific requirement in my case is that I need to be able to disable certain options in the control's menu. I'd like to know if anyone has found a plugin that allows me to do this, prefera开发者_如何学编程bly easily by using something like
$("#selectControl").change(function()
{
$(this).find(":selected").attr("disabled", "disabled");
});
Of course, the attr()
function won't work on a generated element, but is there a jQuery plugin that has a wrapper for this functionality?
EDIT
To make myself clearer, I'll say it differently: I have an HTML structure that looks like a nicely styled select box, with the hidden actual <select>
control behind it. It's possible to disable an <option>
in the <select>
, but not in most jQuery plugins for styling select boxes. I'm asking if there is a plugin out there that will allow me to disable certain options in the prettified <select>
element.
Thanks,
James
I'm not sure I'm tracking on your question, but the code snippet below operates on a dropdown, to hide certain options:
//show them all
$('#<%= ddlRank.ClientID %> span').each(
function () {
var opt = $(this).find("option").show();
$(this).replaceWith(opt);
}
);
//hide unrelated
var selectionLost = false;
$('#<%= ddlRank.ClientID %> option').each(
function () {
if ($.inArray($(this).val(), related) == -1) {
if ($(this).attr("selected")) {
$(this).removeAttr("selected");
selectionLost = true;
}
$(this).wrap("<span>").hide();
}
}
);
The secret sauce (to hide them, in conjunction with the "show all" code) is:
$(this).wrap("<span>").hide();
Well, it turns out I didn't look hard enough. For those who want the same thing as myself, I found this plugin. To get my "disable on select" behaviour, I used the jQuery tie plugin which is linked to on the page in the URL somewhere. This is a bit of a hefty selectbox plugin, but minified it should be fine. The box is styled very easily with CSS and can be done entirely without images using the border width trick. If I get enough interest, I'll write a tutorial on how to style these boxes.
EDIT
The plugin linked above didn't actually work very well, so I just wrote my own. I'll post a link to it some time if anyone wants to have a look.
精彩评论