0
1)以下代碼附加在選定值的一些選項一個選項檢查是否存在選項,添加它們,如果它們不存在,並設置爲「已選擇」
$('#product').on('change',function(){
if($(this).val()!=='Free'){
$("#license option:selected").remove();
$("#license").append("<option value=''> -- Select --</option>").attr('selected','selected');
$("#license").append("<option value='1'>1</option>").val('Licenza');
$("#license").append("<option value='2'>2</option>").val('2');
$("#license").append(" <option value='3'>3</option>").val('3');
}else{
$("#license").find('option').remove().end().append("<option value='free'> Free</option>").val('free');
}
});
的問題是,在每個變化在選擇id =「#product」後,所有4個選項都被追加。 在哪裏我需要插入.length來檢查選項是否存在? 2)另外,如果選項不存在,並且它們被添加到select id =「#license」,我不能將第一個選項值(「--select--」選項)設置爲選中一。
我試圖與
.attr('selected','selected');
,但它不會工作;
希望解釋清楚。 任何幫助將不勝感激。
編輯
這裏有一個link到unterstand情況
是的!就是這個。最後這麼簡單。非常感謝您的幫助! – Someone33 2013-05-14 02:42:41