[英]Modifying data sent by a 3rd party JS
我正在使用第3方庫來填充dropdown
的客戶網站上,SELECT中的代碼為:
<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
完成后,我將如何只運行該功能?
編輯
也嘗試過:
$(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條目。
放置:
$("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();
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.