简体   繁体   English

修改第三方JS发送的数据

[英]Modifying data sent by a 3rd party JS

I am working on a client's website which uses a 3rd party library to populate a dropdown the code in the SELECT is: 我正在使用第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>

That will populate: 这将填充:

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

With all the states (if i would chose United States). 与所有州(如果我会选择美国)。

I would like to then REMOVE entries bassed on the value. 然后,我想删除按该值确定的条目。

The output of selecting US would be like this: 选择美国的输出如下所示:

<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...

Once this has been populated, I would like to remove a few based on Value. 填充完毕后,我想根据“价值”删除一些内容。

So To remove Florida I tried: 因此,要删除佛罗里达州,我尝试过:

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

However due to the fact that JS runs in order and does not wait, this will not work as it is run before the select is ever populated. 但是,由于JS按顺序运行且不等待,因此无法正常运行,因为它在select填充之前就已运行。

How would I go about only running that function AFTER the select has been populated? select完成后,我将如何只运行该功能?

EDIT 编辑

Also Tried: 也尝试过:

$(document).ready(function() {

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

EDIT 2 编辑2

OnChange has been removed, and the code has been moved to .ready() 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();
    });

This method still fills the select full of options, but does not remove the FL entry. 此方法仍会填充select完整选项,但不会删除FL条目。

Place this: 放置:

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

Inside this: 在里面:

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

That'll only remove when the select is changed. 仅在更改选择时将其删除。 Put that in your JS and you'll be golden. 将其放入您的JS中,您将大有作为。

EDIT 编辑

The best solution would be to remove the onchange all together. 最好的解决方案是一起删除onchange Then place the code in your JS, like so: 然后将代码放入您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM