使用每个选项的值更改“更改选择”选项的颜色

时间:2019-08-18 07:05:49

标签: jquery

我有select option,我希望将选项颜色更改为红色,并且将选项值更改为1450 to 1454

<select id='test'>
<option value="1463">SAHABAN CHAK</option>
<option value="1462">SAHABAJPUR</option>
<option value="1461">PAR DEONAPUR SOVAPUR</option>
<option value="1460">LAXMIPUR</option>
<option value="1459">KUMBHIRA</option>
<option value="1458">KRISHNAPUR</option>
<option value="1457">GOLAPGANJ</option>
<option value="1456">CHARI ANANTAPUR</option>
<option value="1455">BIRNAGAR-II</option>
<option value="1454">BIRNAGAR-I</option>
<option value="1453">BHAGABANPUR</option>
<option value="1452">BEDRABAD</option>
<option value="1451">BAKHRABAD</option>
<option value="1450">AKANDABARIA</option>

</select>

`

1 个答案:

答案 0 :(得分:0)

您需要遍历选项,并在相关的地方添加红色

$("#test option").each(function(){
    if((this.value >= 1450) && (this.value <= 1454)){
      $(this).css('background','red');
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id='test'>
<option value="1463">SAHABAN CHAK</option>
<option value="1462">SAHABAJPUR</option>
<option value="1461">PAR DEONAPUR SOVAPUR</option>
<option value="1460">LAXMIPUR</option>
<option value="1459">KUMBHIRA</option>
<option value="1458">KRISHNAPUR</option>
<option value="1457">GOLAPGANJ</option>
<option value="1456">CHARI ANANTAPUR</option>
<option value="1455">BIRNAGAR-II</option>
<option value="1454">BIRNAGAR-I</option>
<option value="1453">BHAGABANPUR</option>
<option value="1452">BEDRABAD</option>
<option value="1451">BAKHRABAD</option>
<option value="1450">AKANDABARIA</option>

</select>