简体   繁体   中英

jQuery attr('id') returns undefined

I have a list of items and when a user clicks on one of them, it gets assigned a class called 'active-child'. Just one item could be selected at the moment, and when the user clicks on another item, this item is cleaned of 'active-child' class and then this class gets assigned to the newly selected item. Just like any other non-multiple list.

Here is the selected element's HTML:

<div id="5" class="new-child-item new-child-item-active active-child"> Software </div>

When I want to submit the selected item's "id" to a php script to process it further, the jquery does not retrieve the id value of the element and says it is undefined . My approach to getting the selected item's id attribute is as follows:

$('.active-child').attr('id');

I also have tested this, but still returns undefined :

$('.active-child:first-child').attr('id');

The strange thing is that I first check to see if the user has selected anything at all, and if yes, then do the ajax, at this moment, the jquery returns 1 , means the user has selected one item. Here is the code block which checks to see the selection and then send it through ajax:

 if($('.active-child').length > 0)
                  {
                      $('.new-item-cats-list-holder').empty();
                       $('.new-item-cats-list-holder').hide()
                  $('.big-loader').show();
                      console.log("This is the id: " + $('.new-child-item-active:first-child').attr('id'));
                      $.ajax({
                      url : base_path('ajax/set_credentials/step_2'),
                      type: "POST",
                      data : ({'set_sess':true, 'category' : $('.active-child').attr('id')}),
                      success : function(msg){                                                                  
                        if(msg!=0)
                        {                                                       
                            //window.location.href = base_path('panel/add/step/ads/3');                     

                        }
                      },
                  });
                  }// end of length if

ID检索销毁和DOM之前,empty()和hide()函数起作用,并且不允许它存在,然后再获取其id值。

I have an inkling that you are using numerical values as IDs, or at least have ID that starts with a number. My suspicion comes from the fact that your console log returns 1, and you have specified in your question that you are logging the active child's ID.

This is invalid — classes and IDs have to start with an alphabet or an underscore or a dash, and then followed by any alphanumeric, underscore or dash characters and a combination thereof.

See here: Which characters are valid in CSS class names/selectors? and http://www.w3.org/TR/CSS21/syndata.html#characters

This is unless you are using HTML5, but you need to declare the doctype correctly: <!DOCTYPE html>

Also, have you checked that your IDs are unique?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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