Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 禁用选择函数Jquery 1.9_Javascript_Jquery - Fatal编程技术网

Javascript 禁用选择函数Jquery 1.9

Javascript 禁用选择函数Jquery 1.9,javascript,jquery,Javascript,Jquery,以下脚本适用于除1.9(ahhh)之外的所有Jquery版本。很简单,我有5个下拉列表,每个列表中有20个相同的选项。每次他们选择一个选项时,该选项将被禁用,无法在下一个列表中选择。在任何人提到使用复选框函数之前,我已经这么做了,效果很好,但是楼上的大个子决定让我重写这个脚本 $("select").change(function () { var $this = $(this); var prevVal = $this.data("prev"); var oth

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

但是,当使用类似“input[value=abc]”的选择器时,它应该 始终按“值”属性选择,而不更改 属性,例如,由用户输入文本。 从jQuery 1.9开始,它的行为正确且一致。早期的 jQuery的版本有时会在需要时使用该属性 已使用该属性。 大宗报价

换句话说”


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

$("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());
});

如果您想看到它的实际运行,可以在这里看到:

您会遇到什么错误?您是否使用jQuery迁移插件检查版本中的更改?您没有提到此代码是如何工作的,尽管在“更改”处理程序的第4行中有一个明显的错误。
“选项[value=“+$(this).val(“”)+]“
没有任何意义使用更多的代码/假数据制作jsbin或JSFIDLE,让我们了解您正在做什么。这是一个很有魅力的剧本@Elijan您的右侧属性和属性在1.7中都有效,但我在1.9中尝试了属性和属性,两者都不起作用。所以现在我真的很困惑,仍然不知道为什么这个脚本在1.9中不能工作。这是一个不起作用的例子
$("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());
});