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
848
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

    0001  耿直BOY 男 1990-02-01 曾经获得迪尼斯全球最能吃奖曾经获得迪尼斯全球最能吃奖 北京 2399 123 github ****** 18511009922 1991-02-01 23 23 23 正常  
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

      迷茫
      如果实例是你发的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
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!