【发布时间】:2013-06-27 18:29:59
【问题描述】:
我使用的 jQuery 函数禁止在一页上选择相同的选择值。例如:如果我在第 2 页有不同的选择,并且第一次选择时选择的值为 1,那么在第二次选择时,值 1 应该被禁用。我找到了注释的 jQuery 函数,但我不知道它的运行方式。
var previous = -1;
// Setting a previously selected value
$("select").change(function () {
// When any select element is changed
if ($(this).val() > -1) {
// When value is > -1
// Get all selects but not the current one which triggered the change event
$("select").not(this)
.find("option[value=" + $(this).val() + "]").attr('disabled', 'disabled');
// set the current option value to disabled
// Get all selects but not the current one which triggered the change event
$("select").not(this)
.find("option[value=" + previous + "]").removeAttr('disabled');
// Remove the disabled property on previous value
} else {
// Get all selects but not the current one which triggered the change event
$("select").not(this)
.find("option[value=" + previous + "]").removeAttr('disabled');
// Remove the disabled property
}
}).focus(function () {
previous = $(this).val();
// store the current value of the select to previous
});
这里是 JSFiddle 链接:http://jsfiddle.net/Z2yaG/4/
有人可以解释一下 var 以前的数组吗?函数如何将所有选择的值存储在 var previous 上?
【问题讨论】:
-
它没有。它只存储一个值。如果你learn how to debug JavaScript,你可以自己设置断点并检查变量。
标签: jquery