2011-10-07 27 views
2

我有一些HTML我正在使用CMS的編程我沒寫,它正在輸出一些HTML無線電選項,並重復它們。不幸的是,我無法控制這個編程的輸出,所以我正在嘗試一種替代路線,只試圖讓一個HTML實例出現。這裏有一個例子:如何過濾掉重複的HTML與jQuery

<label class="fc_radio" for="shipping_service_101"><input type="radio" name="shipping_service" id="shipping_service_101" value="101|45" class="fc_radio fc_required"><span class="fc_shipping_carrier">USPS International Shipping Rate (tracking included)</span> <span class="fc_shipping_cost"><span class="fc_currency_symbol">$</span>45</span> </label> 

<label class="fc_radio" for="shipping_service_101"><input type="radio" name="shipping_service" id="shipping_service_101" value="101|45" class="fc_radio fc_required"><span class="fc_shipping_carrier">USPS International Shipping Rate (tracking included)</span> <span class="fc_shipping_cost"><span class="fc_currency_symbol">$</span>45</span> </label> 

正如你所看到的,它是相同的代碼。無論如何,jQuery基本上過濾掉了正在輸出的額外的HTML,並保留了HTML中的1個實例?他們是這個ID和Class一個div內:

<div id="fc_shipping_methods_inner" class="fc_shipping_methods_inner"> 
REPEATING HTML HERE 
</div> 
+0

您應該解決的問題或有您使用修復它的解決方案的開發。這樣的事情是一個錯誤,你不應該用JS包紮它。 – prodigitalson

+0

同意。必須破解,直到找到解決辦法。絕對不是一個長期的解決方案,但現在仍然是一種解決方案。 – flinx777

回答

0

如果它僅僅是一個額外的元素,你可以這樣做:

$('label.fc_radio').eq(1).remove();

示例:http://jsfiddle.net/ZgLaU/

如果一個以上的元素,使用.slice()

$('label.fc_radio').slice(1).remove();

例2:http://jsfiddle.net/ZgLaU/1/

+0

OP沒有提到最多會有兩個重複的項目。它應該像$('label.fc_radio:gt(0)')。remove(); – Kai

+0

同意@Kai。這就是爲什麼我給了第二個選擇。檢查該小提琴中的HTML。有六個輸入。該代碼刪除了第一個。 –

0

試試這個:

var uniqueRadioIDs = []; 

$('#fc_shipping_methods_inner label.fc_radio').each(function() { 
    if(uniqueRadioIDs.indexOf($(this).attr('for')) == -1) { 
    // unknown yet, add to list 
    uniqueRadioIDs.push($(this).attr('for')); 
    } 
    else 
    { 
    // duplicate 
    $(this).remove(); 
    } 
}); 
+0

嘿雪橇,感謝您的代碼。只是給它一個旋轉,但它不工作:http://jsfiddle.net/flinx777/xmQuD/ ...任何想法? – flinx777

+0

嗨,看起來像jsfiddle此刻被打破。一個簡單的hello世界也會失敗;)除了你需要包裝代碼:'$(document).ready(function(){/ * code goes here * /});' – sled

+0

http://jsbin.com/ecitoq /編輯(點擊渲染) – sled