search

Home  >  Q&A  >  body text

javascript - Why can't jqeury's ID selector with string concatenation be used?

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_u1582phpcn_u15822748 days ago966

reply all(6)I'll reply

  • 学习ing

    学习ing2017-06-30 10:01:52

    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?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-30 10:01:52

    The questioner can judge whether the selector is normal

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

    // check = user1--mycheckbox

    <input id="user1-mycheckbox" .....

    reply
    0
  • 迷茫

    迷茫2017-06-30 10:01:52

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

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-30 10:01:52

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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-30 10:01:52

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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-30 10:01:52

    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.

    reply
    0
  • Cancelreply