繁体   English   中英

修改第三方JS发送的数据

[英]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.

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