2013-07-24 87 views
1

我一個客戶的網站工作,它使用第三方庫來填充dropdown在選擇代碼發送修改數據是:由第三方JS

<select border="0" class="" id="country" style="" name="country" size="1" onchange="ChangeCountryAndStateLabel(
         { 
         'obj'  : this, 
         'targetObj' : '#state_cus1', 
         'sel'  : '', 
         'id'  : 'fields_state' 
         } 
         , 
         { 
         'obj'  : this, 
         'targetObj' : '#fields_state_region', 
         'template' : '<LABEL>:' 
         });" > 
       <option value="" onclick="" >Select Country</option> 
       <option value="38" onclick="" >Canada</option> 
       <option value="223" onclick="" >United States</option></select> 

將填充:

<div id="state_cus1" style="width:165px;"> 
       <input type="text" id="fields_state" name="fields_state" value="" onchange="SetStateHid(this);"/> 
      </div> 

與所有的州(如果我會選擇美國)。

我想然後刪除基於該值的條目。

選擇美國會是這樣的輸出:

<div id="state_cus1" style="width:165px;"> 
       <input type="text" id="fields_state" name="fields_state" value="" onchange="SetStateHid(this);"/> 
      </div> 
<option value="AL" onclick="">Alabama (AL)</option> 
etc etc etc... 

一旦這個已被填充,我想刪除基於價值等等。

所以要去除佛羅里達我想:

$("select > option[value*='FL']").remove(); 

但是,由於在JS爲了運行,不等待,因爲它是在select是不斷填充之前運行,這將無法工作的事實。

我將如何去僅在運行select已填充後運行該功能?

EDIT

也嘗試:

$(document).ready(function() { 

$("#fields_state").on("change", function(){ 
    $("#fields_state > option[value*='FL']").remove(); 
} 

編輯2

OnChange已被移除,以及將碼已被移動到.ready()

ll(document).ready(function() { 
     ll("#country").change(function(){ 
     ChangeCountryAndStateLabel({ 
      'obj'  : this, 
      'targetObj' : '#state_cus1', 
      'sel'  : '', 
      'id'  : 'fields_state' 
     },{ 
      'obj'  : this, 
      'targetObj' : '#fields_state_region', 
      'template' : '<LABEL>' 
     }); 

     ll("#fields_state > option[value*='FL']").remove(); 
    }); 

該方法仍填充了select全部選項,但不刪除FL條目。

+0

添加代碼在最後一個方框只是半和報價之間(; X「)在第一個框中的代碼的onchange ATTRIB不會忘記。以牙籤挑選你的內部引號 – dandavis

+0

像這樣:'}); $(「#fields_state> option [value * ='FL']」)。remove();「 >'? –

+0

是的,但沒有刪除引號:'#fields_state>選項[值* = \'FL']' – dandavis

回答

0

廣場這樣的:

$("select > option[value*='FL']").remove(); 

這裏面:

$("#country").on("change", function(){ 
    $("#fields_state > option[value*='FL']").remove(); 
} 

當選擇改變這隻會刪除。把它放在你的JS裏,你會變成金色的。

編輯

最好的解決辦法是刪除onchange一起。然後將代碼放在你的JS,就像這樣:

$(document).ready(function() { 
    $("#country").on("change", function(){ 
     ChangeCountryAndStateLabel({ 
      'obj'  : this, 
      'targetObj' : '#state_cus1', 
      'sel'  : '', 
      'id'  : 'fields_state' 
     },{ 
      'obj'  : this, 
      'targetObj' : '#fields_state_region', 
      'template' : '<LABEL>' 
     }); 

     $("#fields_state > option[value*='FL']").remove(); 
    }); 
}); 
+0

這是一個不行,添加'.ready()'但是不會在更改後刪除選項,如果它有助於選擇標識爲fields_state,請參閱編輯我已更改的內容 –

+0

我已編輯我的答案以包含標識的 –

+0

刪除OnChange )''並且在'.ready()'中使用你的代碼不起作用,我得到'Uncaught SyntaxError:Unexpected token}' –