简体   繁体   English

按类将 javascript 应用于多个项目

[英]Apply javascript to multiple items by class

I have some select boxes that I want to have color coded.我有一些要进行颜色编码的选择框。 There are three per row, by 18 rows.每行有 3 个,共 18 行。

It is easy to color code the drop down, but I need to use Javascript to color code the display of the selected item.对下拉菜单进行颜色编码很容易,但我需要使用 Javascript 对所选项目的显示进行颜色编码。 I have cobbled together a script that does it, but only for the first one on the page .. and it changes ALL of them to the same color.我拼凑了一个脚本来完成它,但仅适用于页面上的第一个..并将它们全部更改为相同的颜色。 I need the script to apply to each one independently.我需要脚本独立地应用于每个脚本。 I am very much a beginner at Javascript.我是 Javascript 的初学者。 I have figured out that I need to use the var i thing, but I can't get it to work.我发现我需要使用 var i 东西,但我无法让它工作。

<td>
    <div>
        <select type="" name="tee_condition" class="selectcondition"  id="tee_condition">
            <option style="color: #5cb85c;"  value="Excellent"<?php if($listresult['tee_condition'] == 'Excellent'){echo 'selected';} ?> >Excellent</option>
            <option style="color: #0275d8;" value="Good"<?php if($listresult['tee_condition'] == 'Good'){echo 'selected';} ?> >Good</option>
            <option style="color: #f0ad4e;" value="Fair"<?php if($listresult['tee_condition'] == 'Fair'){echo 'selected';} ?> >Fair</option>
            <option style="color: #ff0000;" value="Poor"<?php if($listresult['tee_condition'] == 'Poor'){echo 'selected';} ?> >Poor</option>                                    
        </select>                                               
    </div>
</td>
<td class="text-center">
    <div>
        <select type="" name="fairway_condition" class="selectcondition"  id="fairway_condition">
            <option style="color: #5cb85c;"  value="Excellent"<?php if($listresult['fairway_condition'] == 'Excellent'){echo 'selected';} ?> >Excellent</option>
            <option style="color: #0275d8;" value="Good"<?php if($listresult['fairway_condition'] == 'Good'){echo 'selected';} ?> >Good</option>
            <option style="color: #f0ad4e;" value="Fair"<?php if($listresult['fairway_condition'] == 'Fair'){echo 'selected';} ?> >Fair</option>
            <option style="color: #ff0000;" value="Poor"<?php if($listresult['fairway_condition'] == 'Poor'){echo 'selected';} ?> >Poor</option>                                    
        </select>                                               
    </div>
</td>
<td class="text-center">                                                
    <div>
        <select type="" name="green_condition" class="selectcondition"  id="green_condition">
            <option style="color: #5cb85c;"  value="Excellent"<?php if($listresult['green_condition'] == 'Excellent'){echo 'selected';} ?> >Excellent</option>
            <option style="color: #0275d8;" value="Good"<?php if($listresult['green_condition'] == 'Good'){echo 'selected';} ?> >Good</option>
            <option style="color: #f0ad4e;" value="Fair"<?php if($listresult['green_condition'] == 'Fair'){echo 'selected';} ?> >Fair</option>
            <option style="color: #ff0000;" value="Poor"<?php if($listresult['green_condition'] == 'Poor'){echo 'selected';} ?> >Poor</option>                                  
        </select>                                               
    </div>
</td>

And my javascript is:我的 javascript 是:

    //**** Script to Color the Dropdowns
$(document).ready(function () {
    colorSelect(); // call this first so we start out with the correct Color
    // this will call our function every time the selection value of the field changes
    $(".selectcondition").change(function () {
        colorSelect();
    });

});
// this changes the color of the selected item
function colorSelect() {
    if ($(".selectcondition").val() === "Excellent"){
        $('.selectcondition').css('color','#5cb85c');
    }else if($(".selectcondition").val() === "Good"){
        $('.selectcondition').css('color','#0275d8');
    }else if($(".selectcondition").val() === "Fair"){
        $('.selectcondition').css('color','#f0ad4e');
    }else {
        $('.selectcondition').css('color','#FF0000');
    }
}

.css will set the CSS of all matching elements. .css将设置所有匹配元素的 CSS。 Iterate over the selects with each instead, and get a reference to the current one you're iterating over with this .使用each迭代选择,并获取对您正在使用this迭代的当前选择的引用。

You can also make the code much more concise by using an object indexed by option values instead of repetitive if / else statements:您还可以通过使用由选项值索引的对象而不是重复的if / else语句来使代码更加简洁:

 function colorSelect() { const colors = { Excellent: '#5cb85c', Good: '#0275d8', Fair: '#f0ad4e', Poor: '#FF0000' } $(".selectcondition").each(function() { $(this).css('color', colors[this.value]); }); } colorSelect(); $(".selectcondition").change(colorSelect);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <td> <div> <select type="" name="tee_condition" class="selectcondition" id="tee_condition"> <option style="color: #5cb85c;" value="Excellent" <?php if($listresult[ 'tee_condition']=='Excellent' ){echo 'selected';} ?> >Excellent</option> <option style="color: #0275d8;" value="Good" <?php if($listresult[ 'tee_condition']=='Good' ){echo 'selected';} ?> >Good</option> <option style="color: #f0ad4e;" value="Fair" <?php if($listresult[ 'tee_condition']=='Fair' ){echo 'selected';} ?> >Fair</option> <option style="color: #ff0000;" value="Poor" <?php if($listresult[ 'tee_condition']=='Poor' ){echo 'selected';} ?> >Poor</option> </select> </div> </td> <td class="text-center"> <div> <select type="" name="fairway_condition" class="selectcondition" id="fairway_condition"> <option style="color: #5cb85c;" value="Excellent" <?php if($listresult[ 'fairway_condition']=='Excellent' ){echo 'selected';} ?> >Excellent</option> <option style="color: #0275d8;" value="Good" <?php if($listresult[ 'fairway_condition']=='Good' ){echo 'selected';} ?> >Good</option> <option style="color: #f0ad4e;" value="Fair" <?php if($listresult[ 'fairway_condition']=='Fair' ){echo 'selected';} ?> >Fair</option> <option style="color: #ff0000;" value="Poor" <?php if($listresult[ 'fairway_condition']=='Poor' ){echo 'selected';} ?> >Poor</option> </select> </div> </td> <td class="text-center"> <div> <select type="" name="green_condition" class="selectcondition" id="green_condition"> <option style="color: #5cb85c;" value="Excellent" <?php if($listresult[ 'green_condition']=='Excellent' ){echo 'selected';} ?> >Excellent</option> <option style="color: #0275d8;" value="Good" <?php if($listresult[ 'green_condition']=='Good' ){echo 'selected';} ?> >Good</option> <option style="color: #f0ad4e;" value="Fair" <?php if($listresult[ 'green_condition']=='Fair' ){echo 'selected';} ?> >Fair</option> <option style="color: #ff0000;" value="Poor" <?php if($listresult[ 'green_condition']=='Poor' ){echo 'selected';} ?> >Poor</option> </select> </div> </td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM