jquery
  • html
  • 2013-05-31 47 views 0 likes 
    0

    我試圖通過jquery的 動態創建一個選擇框創建選擇框,但它不應該有電流值無電流值jQuery中

    jQuery('.copyad').each(function() { ads.push(jQuery(this).attr('id')); }); // ["ad1","ad2","ad3"] 
    jQuery.each(ads , function(index, value) { 
         option += '<option value="'+value+'">Copy To Ad '+value+'</option>'; 
        }); 
    jQuery('.copyad').html(option); 
    

    它使輸出像

    Ad1 === 
    <select class="copyad" id="ad1" > 
    <option value="ad1">Copy To ad1 </option> 
    <option value="ad2">Copy To ad2 </option> 
    <option value="ad3">Copy To ad3 </option> 
    </select> 
    
    Ad2 === 
    <select class="copyad" id="ad2" > 
    <option value="ad1">Copy To ad1 </option> 
    <option value="ad2">Copy To ad2 </option> 
    <option value="ad3">Copy To ad3 </option> 
    </select> 
    Ad3 === 
    <select class="copyad" id="ad3" > 
    <option value="ad1">Copy To ad1 </option> 
    <option value="ad2">Copy To ad2 </option> 
    <option value="ad3">Copy To ad3 </option> 
    </select> 
    

    但我需要一個像

    Ad1 === 
    <select class="copyad" id="ad1" > 
    <option value="ad2">Copy To Ad 2 </option> 
    <option value="ad3">Copy To Ad 3 </option> 
    </select> 
    
    Ad2 === 
    <select class="copyad" id="ad2" > 
    <option value="ad1">Copy To Ad 1 </option> 
    <option value="ad3">Copy To Ad 3 </option> 
    </select> 
    
    Ad3 === 
    <select class="copyad" id="ad3" > 
        <option value="ad1">Copy To Ad 1 </option> 
        <option value="ad2">Copy To Ad 2 </option> 
    </select> 
    

    所以在輸出...

    我怎樣才能做到這一點

    回答

    0
    jQuery('.copyad').each(function() { ads.push(jQuery(this).attr('id')); }); 
    jQuery.each(ads , function(index, value) { 
        if(ads[index] != value){ 
         option += '<option value="'+value+'">Copy To Ad '+ value[value.length -1] +'</option>'; 
        } 
    }); 
    jQuery('.copyad').html(option); 
    

    嘗試添加如果條件這樣的..

    +0

    ans被修改,看看它.. – Paritosh

    0

    如果我理解正確的,你只是想連接下面的id「廣告」部分的數量:

    option += '<option value="'+value+'">Copy To Ad '+ value.substr(value.length - 1) +'</option>'; 
    

    *編輯:基於下面的評論:

    jQuery.each(ads , function(index, value) { 
        if(ads[index] != value) { 
         option += '<option value="'+value+'">Copy To Ad '+ value.substr(value.length - 1) +'</option>'; 
        } 
    }); 
    
    +0

    是的,但我想從廣告1中的選擇框中刪除當前的廣告價值,應該只有廣告2和廣告3不是廣告1 ...... –

    相關問題