2014-07-02 41 views
2

我隱藏了從某些池/組中刪除標記的輸入。刪除並存儲多個屬性

<input type="hidden" name="Remove[<?= $i; ?>][Badge][id]" value="<?= $badge['id']; ?>" data-id="<?= $badge['id']; ?>" /> 

我是比較新的JS和JQuery但這裏是我迄今爲止...

$('.glyphicon-remove').click(function(){ 
     $(this).parent().toggleClass('remove'); 
     $(this).siblings().removeAttr("name value data-id"); 
     return false; 
    }); 

的問題是,當他們點擊.glyphicon-remove我需要它來切換這些屬性。我也玩過JQ功能分離,但無法將它與attr()標籤一起使用。

我需要能夠在每次點擊時切換每個屬性。請讓我知道你是否有任何建議,或者我是否可以澄清我的問題。

+0

您使用的是jQuery 1.7或以上版本嗎? '$(this).parent()'返回正確的元素嗎? '$(this).siblings()'返回正確的元素嗎? –

+0

jQuery UI - v1.8.21。兄弟姐妹和父母選擇器都會返回正確的元素。 – woodfordreserve

+0

'.attr({name:「」, value:「」,data-id:「」 });'' – zer00ne

回答

0

您需要某種toggleAttr保留舊值才能恢復。 請嘗試以下操作:

$('.glyphicon-remove').click(function(){ 
    $(this).parent().toggleClass('remove'); 
    var sib = $(this).siblings(); 
    if ($(this).parent().hasClass('remove')) { 
     //Stores attrs to correspontent data-* attribute 
     sib.each(function(){ 
      $(this).data("name", $(this).attr("name")); 
      $(this).data("value", $(this).attr("value")); 
      $(this).data("data-id", $(this).attr("data-id")); 
     }); 
     sib.removeAttr("name value data-id"); 
    } else { 
     //Restores attrs from the correspondent data-* attribute 
     sib.each(function(){ 
      $(this).attr("name", $(this).data("name")); 
      $(this).attr("value", $(this).data("value")); 
      $(this).attr("data-id", $(this).data("data-id")); 
     }); 
     sib.removeData("name"); 
     sib.removeData("value"); 
     sib.removeData("data-id"); 
    } 
    return false; 
});