简体   繁体   English

jQuery动态对象加载选择器

[英]JQuery Dynamic Objects load selectors

I've a form when I click one radio button to load a subform. 单击一个单选按钮以加载子窗体时,我有一个窗体。 Ok this work perfectly, but has 3 selectors that I need external data when only this subform loaded. 好的,这工作正常,但是有3个选择器,当仅加载此子窗体时,我需要外部数据。 So, I did in this way: 所以,我这样做:

$(document).on('focus', '#reemb', function () {
    $.getJSON("/banks.php", function (json) {
        $('#bank').empty();
        $.each(json, function (i, obj) {
            $('#bank').append($('<option>').text(obj.name).attr('value', obj.code));
        });
    });

    $.getJSON('/statecity.json', function (data) {
        var items = [];
        var options = '<option value="">State</option>';
        $.each(data, function (key, val) {
            options += '<option value="' + val.name + '">' + val.name + '</option>';
        });
        $("#state").html(options);

        $("#state").change(function () {

            var options_city = '';
            var str = "";

            $("#state option:selected").each(function () {
                str += $(this).text();
            });

            $.each(data, function (key, val) {
                if (val.name == str) {
                    $.each(val.city, function (key_city, val_city) {
                        options_city += '<option value="' + val_city + '">' + val_city + '</option>';
                    });
                }
            });
            $("#city ").html(options_city);

        }).change();

    });
});

This work fine, but everytime that I need to change one date the selectors clear and load again. 这项工作正常,但是每次我需要更改日期时,选择器都会清除并再次加载。

I tried to add tag onload to start the function to load selectors in this subform, but don't works. 我尝试添加标签onload来启动此子窗体中的加载选择器的功能,但是不起作用。 Also tried change events to .on, but also don't work. 还尝试将事件更改为.on,但也不起作用。 How I need to do this? 我该怎么做?

Thx!! 谢谢!!

Not knowing what #reemb is, I would empty the relevant sub selects: 不知道什么是#reemb ,我将清空相关子选择:

If the container holding the selects is ALSO emptied, you need to delegate all even handlers of objects inside too - like $(document).on("change","#bank", function() { 如果保存选择的容器也被清空,则您还需要委派对象内部的所有偶数处理程序,例如$(document).on("change","#bank", function() {

$(document).on('click', '#reemb', function() {
  $.getJSON("/banks.php", function(json) {
    $('#bank').empty();
    $.each(json, function(i, obj) {
      $('#bank').append($('<option>').text(obj.name).attr('value', obj.code)).change();
    });
  });

  $('#bank').on("change", function() {
    $('#city').empty();
    $.getJSON('/statecity.json', function(data) {
      var items = [];
      var options = '<option value="">State</option>';
      $.each(data, function(key, val) {
        options += '<option value="' + val.name + '">' + val.name + '</option>';
      });
      $("#state").html(options).change(); // if needed
    });

    $("#state").on("change", function() {
      var options_city = '';
      var str = "";
      $("#state option:selected").each(function() {
        str += $(this).text();
      });
      $.each(data, function(key, val) {
        if (val.name == str) {
          $.each(val.city, function(key_city, val_city) {
            options_city += '<option value="' + val_city + '">' + val_city + '</option>';
          });
        }
      });
      $("#city ").html(options_city).change(); // if needed
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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