http://jsbin.com/edOJurI/1/edit
Вариант один работает по назначению, но мой второй вариант, похоже, не работает, и путается, почему это не так.
Буду признателен за любую оказанную помощь.
JavaScript:
$(window).load(function() {
var enabled = true;
function calculation(e) {
// Colors
$("select#colors").each(function() {
if ($(this).val() === 'Black') {
enabled = true;
$('span#1').text('0');
}
if ($(this).val() === 'Brown') {
enabled = true;
$('span#1').text('1');
}
if ($(this).val() === 'Red') {
enabled = true;
$('span#1').text('2');
}
if ($(this).val() === 'Orange') {
enabled = true;
$('span#1').text('3');
}
if ($(this).val() === 'Yellow') {
enabled = true;
$('span#1').text('4');
}
if ($(this).val() === 'Green') {
enabled = true;
$('span#1').text('5');
}
if ($(this).val() === 'Blue') {
enabled = true;
$('span#1').text('6');
}
if ($(this).val() === 'Violet') {
enabled = true;
$('span#1').text('7');
}
if ($(this).val() === 'Grey') {
enabled = true;
$('span#1').text('8');
}
if ($(this).val() === 'White') {
enabled = true;
$('span#1').text('9');
}
return false;
});
$("select#digits").each(function() {
if ($(this).val() === 'Black') {
enabled = true;
$('span#2').text('0');
}
if ($(this).val() === 'Brown') {
enabled = true;
$('span#2').text('1');
}
if ($(this).val() === 'Red') {
enabled = true;
$('span#2').text('2');
}
if ($(this).val() === 'Orange') {
enabled = true;
$('span#2').text('3');
}
if ($(this).val() === 'Yellow') {
enabled = true;
$('span#2').text('4');
}
if ($(this).val() === 'Green') {
enabled = true;
$('span#2').text('5');
}
if ($(this).val() === 'Blue') {
enabled = true;
$('span#2').text('6');
}
if ($(this).val() === 'Violet') {
enabled = true;
$('span#2').text('7');
}
if ($(this).val() === 'Grey') {
enabled = true;
$('span#2').text('8');
}
if ($(this).val() === 'White') {
enabled = true;
$('span#2').text('9');
}
return false;
});
}
$(document).ready(function(e) {
calculation(e);
});
$(document).change(function(e) {
calculation(e);
});
});
Я внес некоторые изменения в свой код, чтобы он был еще проще
HTML
<th>
<select id="colors">
<option value="" required>1st Band</option>
<option style="background:black; color:white;" value="0">Black</option>
<option style="background:brown; color:white;" value="1">Brown</option>
<option style="background:red; color:white;" value="2">Red</option>
<option style="background:orange; color:white;" value="3">Orange</option>
<option style="background:yellow; color:black;" value="4">Yellow</option>
<option style="background:green; color:white;" value="5">Green</option>
<option style="background:blue; color:white;" value="6">Blue</option>
<option style="background:violet; color:black;" value="7">Violet</option>
<option style="background:grey; color:white;" value="8">Grey</option>
<option style="background:white; color:black;" value="9">White</option>
</select>
</th>
<th>
<select id="digits">
<option value="" required>2nd Band</option>
<option style="background:black; color:white;" value="0">Black</option>
<option style="background:brown; color:white;" value="1">Brown</option>
<option style="background:red; color:white;" value="2">Red</option>
<option style="background:orange; color:white;" value="3">Orange</option>
<option style="background:yellow; color:black;" value="4">Yellow</option>
<option style="background:green; color:white;" value="5">Green</option>
<option style="background:blue; color:white;" value="6">Blue</option>
<option style="background:violet; color:black;" value="7">Violet</option>
<option style="background:grey; color:white;" value="8">Grey</option>
<option style="background:white; color:black;" value="9">White</option>
</select>
</th>
JAVASCRIPT
function calculation() {
// Colors
$("select#colors").each(function() {
var color = $(this).val();
if(!color)
return;
$('span#1').text(color);
});
$("select#digits").each(function() {
var color = $(this).val();
if(!color)
return;
$('span#2').text(color);
});
}
$(document).ready(function() {
calculation();
});
$(document).change(function(){
calculation();
});
Вы можете очень упростить свой код, если вы немного измените структуру HTML.
<select id="colors">
<option>1st Band</option>
<option style="background:black; color:white;" value="0">Black</option>
<option style="background:brown; color:white;" value="1">Brown</option>
...........
обратите внимание на значение в теге "option" (0,1,2...)
а затем в js:
$("select#colors").each(function() {
$('span#1').text($(this).val());
});
Просто ознакомьтесь с рабочей демонстрацией здесь:
Вы добавили id="digits"
к вашему <option>
под <select>
.
<select>
<option id="digits">2nd Band</option>
должно быть
<select id="digits">
<option>2nd Band</option>
+ Изменить
<select>
<option id="digits">2nd Band</option>
чтобы:
<select id="digits">
<option>2nd Band</option>
.each
для селектора идентификаторов? Идентификаторы должны быть уникальными.