简体   繁体   English

如何在Javascript中获取.val()的最后一个字符

[英]How to get last character of .val() in Javascript

I want to apply different background color to my select elements based on the selected option.我想根据所选选项对我的选择元素应用不同的背景颜色。

I have this piece of code that I want to apply to my case:我有这段代码要应用于我的案例:

$('select').on('change', function(ev) {
    $(this).attr('class', '').addClass($(this).children(':selected').val());
});

The problem is that in my case I don't want the complete value of selected option.问题是,在我的情况下,我不想要所选选项的完整值。 But only the last character of the value returned by .val()但只有 .val() 返回值的最后一个字符

I have tried我试过了

$(this).attr('class', '').addClass($(this).children(':selected').val().id.slice(-1));

But it doesn't work但它不起作用

I took this code from this Example This works as I want also, but in my case the value of the option is different for every option, and I have hundreds of selects and options.我从这个示例中获取了这段代码这也可以按我的意愿工作,但在我的情况下,每个选项的选项值都不同,并且我有数百个选择和选项。 In this format:在这种格式中:

Name - Date - 1 Letter Variable

So I have the CSS like this:所以我有这样的 CSS:

select, option { background: #fff; }
select.P, option[value$='P'] { background: yellow; }
select.O, option[value$='O'] { background: lightblue; }
select.A, option[value$='A'] { background: lightgreen; }
select.R, option[value$='R'] { background: salmon; }

And an example of select:和一个选择的例子:

<select onchange='myFunction()' id='" . $i . "' name='" . $i . "' style='width: 80px;'>
        <option value='' style='background:white' style='width: 80px;'></option>
        <option value='" . $vacationIdP . "' style='background:yellow' style='width: 80px;'>P</option>
        <option value='" . $vacationIdO . "' style='background:lightblue' style='width: 80px;'>O</option>
        <option value='" . $vacationIdA . "' style='background:lightgreen' style='width: 80px;'>A</option>
        <option value='" . $vacationIdR . "' style='background:salmon' style='width: 80px;'>R</option>
</select>

The problem is that you are applying slice on undefined .问题是您在undefined上应用slice .val().id is undefined . .val().idundefined
In your html you shouldn't get last char.Its not last.在您的html您不应该得到最后一个字符。它不是最后一个。 Use this用这个

$('select').on('change', function(ev) {
    $(this).attr('class', '').addClass($(this).children(':selected').val().split('.')[1].trim().slice(-1));
});

JS Fiddle JS小提琴

You can use chartAt您可以使用chartAt

 $('select').on('change', function(ev) { let k = $(this).children(':selected').val(); console.log(k.charAt(k.length - 1)) //$(this).attr('class', '').addClass($(this).children(':selected').val()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select> <option value='abc'> ABC</option> <option value='def'>DEF</option> <option value='ghi'> GHI</option> </select>

Try val().pop();尝试 val().pop(); Should return the last element.应该返回最后一个元素。

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

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