Как обработать все значения data на странице?

usergeyv

Знаток
Регистрация
5 Июл 2013
Сообщения
152
Реакции
25
html
HTML:
<div id="test" data-id="12">
<div id="test" data-id="123">
<div id="test" data-id="1234">
<div id="test" data-id="12345">
       
<div id="12"></div>
<div id="123"></div>
<div id="1234"></div>
<div id="1234"></div>
делаю
HTML:
jQuery(function(){   
var theId = $('#test').data("id");   
        $.ajax({
        url:     url,
        type:     "POST", 
        dataType: "html", 
        data: {news: theId},  
        success: function(response) {
            result = $.parseJSON(response);
       
        if (result.status == 'ERROR') {
              $('#'+theId).html('<a href="http://site.ru/404" target="_blank"></a>');
             } else if (result.status == 'OK') {
          $('#'+theId).html('<a href="http://site.ru/' + result.id_news +'" target="_blank"></a>');
            }

        },
        error: function(response) {
        
      $('#'+theId).html('<a href="http://site.ru/404" target="_blank"></a>');
        }
     });   
       
});
Отрабатывает только первое значения то есть "12",как обработать все значения на странице?
 
А вас не смущает, что у вас у всех элементов одинаковый ID? (Привет от быдлокода)
 
вот так сделал
HTML:
var value = $('[data-id]').map(function() {
return this.dataset.id;
});

$.each(value, function( index, theId ) {
//код
});
норм?
 
Назад
Сверху