以下脚本适用于除 1.9 (ahhh) 之外的所有 Jquery 版本。这很简单,我有 5 个下拉列表,每个列表中有相同的 20 个选项。每次他们选择一个选项时,它都会被禁用,并且不能在下一个列表中选择。在有人提到使用复选框功能的想法之前,我已经这样做了,而且效果很好,但是楼上的大个子决定他们希望我重写这个脚本更可取。
$("select").change(function () {
var $this = $(this);
var prevVal = $this.data("prev");
var otherSelects = $("select").not(this);
otherSelects.find("option[value=" + $(this).val('') + "]").prop('disabled', true);
if (prevVal) {
otherSelects.find("option[value=" + prevVal + "]").prop('disabled', false);
}
$this.data("prev", $this.val());
});
function clearForm(form) {
// iterate over all of the inputs for the form
// element that was passed in
$(':input', form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
// checkboxes and radios need to have their checked state cleared
// but should *not* have their 'value' changed
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = -1;
});
};