简体   繁体   English

如何将复选框选中的字符串值与 li 元素进行比较以编辑最后一个

[英]how to compare checkbox checked string value with li element to edit last one

How can I compare checkbox checked string value with li element to edit last one.如何将复选框选中的字符串值与 li 元素进行比较以编辑最后一个。 For example.例如。

 jQuery(function($){

// start ajax 
    $('#filter input[type="checkbox"]').on('change', function(){
        var filter = $('#filter');

                        /* Stuff to do every *odd* time the element is clicked */
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').css('opacity', '1').text('Загрузка...'); 
                // changing the button label
            },
            success:function(data){
                filter.find('button').css('opacity', '0');; // changing the button label back
                if($(".checkbox:checked").length == 0) {
                                $('#response').empty(); 
                          } else {
                                $('#response').html(data); 
                                
                                 };
                                 
                                 $(".usage-product-list li").each(function () {
                                        if ($(this).text() == 'Усиление иммунитета') {
                                            $(this).css('color', 'red');
                                        }
                                    });


                                }
                    });
        return false;
                    });
//end jquery ajax

    // load more toggle start
    $('.load-more').toggle(function() {
    $(this).html('Меньше параметров <i class="fas fa-angle-up"></i>');
    $('.row-wrap').slideDown('slow');
}, function() {
    $(this).html('Больше параметров <i class="fas fa-angle-down"></i>');
    $('.row-wrap').slideUp('slow');
});
    // load more toggle end


    $('#filter input[type="checkbox"]').click(function () {
            $(this).siblings('span').toggleClass('active');
        });
    // end of stack
    });
// end of stack

In my code I have to change the color of li if it contains a text which I put into the string, but how to change every li color with text string equal to each checked checkbox parent LABEL string value?在我的代码中,如果 li 包含我放入字符串中的文本,我必须更改它的颜色,但是如何使用文本字符串更改每个 li 颜色等于每个选中的复选框父 LABEL 字符串值?

 $(".usage-product-list li").each(function () {
                                            if ($(this).text() == 'Усиление иммунитета') {
                                                $(this).css('color', 'red');
                                            }
                                        });

  <label><span></span><input class="checkbox" type="checkbox" name="usage_uluchpoedaemost"/>Улучшение поедаемости</label>
                    <label><span></span><input class="checkbox" type="checkbox" name="usage_usilimmun"/>Усиление иммунитета</label>

Just add this to your success function instead of the $(".usage-product-list li").each(function () {});只需将此添加到您的success function 而不是$(".usage-product-list li").each(function () {}); :

let checked = $("input[type='checkbox']:checked")
checked.each(function() {
   let labelText = $(this).closest("label").text();
    $(".usage-product-list li").each(function() {
       if ($(this).text() == labelText) {
          $(this).css('color', 'red');
       }
    });
});

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

相关问题 JavaScript 如何从一个 function 中获取目标值并乘以已选中复选框的元素值 - JavaScript how to take target value from one function and multiply by element value for which the checkbox has been checked 比较字符串和每个li元素 - Compare string to each li element 选中最后一个值时如何选择其他复选框 - How to select other checkbox when the last value is checked 如何撤消最后选中的复选框? - How to undo the last checked checkbox? 如何最后不允许选中1个复选框 - How to not allow 1 checkbox to be checked at last 选中具有特定值的复选框时如何显示元素? - How to display an element when the checkbox with certain value is checked? AngularJs - <ul><li> 选中其复选框时,元素到顶部 - AngularJs - <ul> <li> element to top when its checkbox is checked 如何从复选框数组中检查至少一个复选框,并且复选框中的文本区域不为空白,且其值为“ other”? - How to check at least one checkbox is checked from the array of checkboxes and the text area is not blank on checkbox with value “other” is checked? 如何修复循环以检查是否选中了复选框,以及是否将值附加到字符串? - How to fix loop to check if checkbox is checked, and if so append value to string? 获取在Gridview中选中的复选框上的元素值? - get element value on checkbox checked in Gridview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM