0

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);
    });
});
4

4 回答 4

0

你添加id="digits"到你的<option><select>

<select>
<option id="digits">2nd Band</option>

应该

<select id="digits">
<option>2nd Band</option>
于 2013-11-10T18:23:30.957 回答
0

改变

            <select>
                <option id="digits">2nd Band</option>

至:

            <select id="digits">
                <option>2nd Band</option>
于 2013-11-10T18:23:49.560 回答
0

我对您的代码进行了一些更改,使其更简单

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();
});
于 2013-11-10T18:37:54.937 回答
0

如果你稍微改变一下你的 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>
    ...........

注意“选项”标签中的值(0,1,2 ...)

然后在js中:

    $("select#colors").each(function() {            
    $('span#1').text($(this).val());
});

只需在这里查看一个工作演示:

http://jsbin.com/edOJurI/5/edit

于 2013-11-10T18:38:36.140 回答