javascript - Why can't jqeury's ID selector with string concatenation be used?
phpcn_u1582
phpcn_u1582 2017-06-30 09:59:52
0
6
974
for(var i = 0; i < checkeds.length; i++){
    var check = 'user'+checkeds[i]+'--mycheckbox';
    $("#"+check).parent('td').siblings('.text-status').removeClass('text-green');
    console.log($("#"+check).parent('td'));
}

In this code, checkeds is an array of ids. The third line is to find elements with the .text-status style in the sibling elements of the parent of the id element and remove the text-green style.
But it cannot take effect. The following is the content of the parent element td reported by the console;

[prevObject: n.fn.init, context: document]
context
:
document
length
:
0
prevObject
:
n.fn.init
__proto__
:
Object(0)

This is the hierarchical relationship of HTML

<tr>
                                <td>
                                    <input id="user1-mycheckbox" name="user-contorl" data-color="yellow" type="checkbox" class="checkbix" data-text="">
                                </td>
                                <td>0001</td>
                                <td><img src="images/user.png" alt=""></td>
                                <td>耿直BOY</td>
                                <td>男</td>
                                <td>1990-02-01</td>
                                <td class="table-text">曾经获得迪尼斯全球最能吃奖曾经获得迪尼斯全球最能吃奖</td>
                                <td>北京</td>
                                <td>2399</td>
                                <td>123</td>
                                <td>github</td>
                                <td>******</td>
                                <td>18511009922</td>
                                <td>1991-02-01</td>
                                <td class="text-blue">23</td>
                                <td class="text-blue">23</td>
                                <td class="text-blue">23</td>
                                <td class="text-green">正常</td>
                                <td><a href="" class="user-edit"><i class="fa fa-edit"></i></a></td>
                            </tr>
phpcn_u1582
phpcn_u1582

reply all(6)
学习ing

Just open the simplest demo and try it out, you will know, it can be spliced,

I can only say where is the problem with the code on your page


Look at the id in the html as user1-mycheckbox

Looking at your splicing, it is 'user'+checkeds[i]+'--mycheckbox';

One is a dash, the other is a double dash, or is the code posted wrong?

伊谢尔伦

The questioner can judge whether the selector is normal

var check = 'user'+checkeds[i]+'--mycheckbox';

// check = user1--mycheckbox

<input id="user1-mycheckbox" .....
迷茫
如果实例是你发的html的话,
1.页面中并没有td.text-status
2.check拼接的时候多了一个短横线
    id="user1-mycheckbox"    //html中
    var check = 'user'+checkeds[i]+'--mycheckbox';    // check= user1--mycheckbox
漂亮男人

It can be used. If it cannot be used, it can only be because the ID does not exist on your page

phpcn_u1582

Can the questioner please send me the test code? This situation usually means that you have not found the right level

过去多啦不再A梦

Check the output to see if it is the result you want. If so, check again whether the id exists on the page. The element with this id must also exist on the page at the beginning and cannot be a future element! Future elements need to use event delegation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template