在jquery中创建没有当前值的选择框

时间:2013-05-31 08:36:36

标签: jquery html

我正在尝试通过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>

所以......

我该怎么做

2 个答案:

答案 0 :(得分: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);

尝试添加if this like ..

答案 1 :(得分: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>';
   }
});