在jquery的下拉列表中选择特定选项
我试着制作一个小脚本,在一系列下拉框中只能使用一次数字,所以。。 [1] [3] [2] [2] [3] [1] [2] [1] [3] 一切都很好,但不是 [2] [2] [1] [1] [1] [3] 我想我已经做了一半了在jquery的下拉列表中选择特定选项,jquery,Jquery,我试着制作一个小脚本,在一系列下拉框中只能使用一次数字,所以。。 [1] [3] [2] [2] [3] [1] [2] [1] [3] 一切都很好,但不是 [2] [2] [1] [1] [1] [3] 我想我已经做了一半了 $("#left").focus(function () { // Store the current value on focus, before it changes previous = this.value; }).change(functi
$("#left").focus(function () {
// Store the current value on focus, before it changes
previous = this.value;
}).change(function() {
// Do soomething with the previous value after the change
var num1 = $("#left option:selected").text();
if ( $("#middle option:selected").text() == num1){
$("#middle option:selected").text(previous)
}
if ( $("#right option:selected").text() == num1){
$("#right option:selected").text(previous)
}
});
以下是完整内容的链接:
它的工作有一点,但我意识到下拉选项开始改变。如果你玩够了,你会注意到我看到了什么
我还注意到我是如何重复大量代码的。如果有更好的书写方法,请告诉我。试试这个
$("#left").focus(function () {
// Store the current value on focus, before it changes
previous = this.value;
}).change(function() {
// Do soomething with the previous value after the change
var num1 = $("#left option:selected").text();
if ( $("#middle option:selected").text() == num1){
$("#middle option[value='"+previous+"']").attr('selected','selected')
}
if ( $("#right option:selected").text() == num1){
$("#right option[value='"+previous+"']").attr('selected','selected')
}
});
此代码在中间和右侧选择框中选择值等于上一个变量值的选项。
向选项添加值属性,如下所示
<option value='2'>2</option>
你查过什么了吗。文本是做什么的?实际上我刚刚意识到OP不想基于选项值进行评估。没有的值属性。我想这使得我的答案根本不正确。。。我不知道为什么,但玩了它之后,它似乎搞砸了。您的代码是有意义的,但它似乎不是一直运行它。试着只玩一个盒子。你会发现它会复制自己。
$("#left").focus(function () {
// Store the current value on focus, before it changes
previous = $(this).val();
}).change(function () {
// Do soomething with the previous value after the change
var num1 = $("#left option:selected").val();
if ($("#middle option:selected").val() == num1) {
// $('body').append(previous);
$("#middle").val(previous)
}
if ($("#right option:selected").val() === num1) {
$("#right").val(previous);
}
});