0

以下脚本适用于除 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;
 });
 };
4

2 回答 2

0

http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

但是,当使用像“input[value=abc]”这样的选择器时,它应该始终通过 value 属性进行选择,而不是用户对属性进行的任何更改,例如从他们输入文本输入。 从 jQuery 1.9 开始,它的行为正确且一致。早期版本的 jQuery 有时会在应该使用属性时使用该属性。 块引用

换句话说”

otherSelects.find("option[value=" + $(this).val('') + "]").attr('disabled', 'disabled')

otherSelects.find("option[value=" + prevVal + "]").removeAttr('disabled');
于 2013-05-30T16:08:14.490 回答
0

如果有人需要使用这种类型的脚本,我想我会添加这个问题的答案:

$("select").change(function () {
    var $this = $(this);
    var prevVal = $this.data("prev");
    var otherSelects = $("select").not(this);
 var thisVal = $(this).val(); 

otherSelects.find("option:contains(" + thisVal + ")").attr('disabled', true);

    if (prevVal) {

        otherSelects.find("option:contains(" + prevVal + ")").attr('disabled', false);
    }

    $this.data("prev", $this.val());
});

如果您想看到它的实际效果,请访问:http: //jsfiddle.net/abigaild12/UQA3X/1/

于 2013-07-22T00:30:55.853 回答