简体   繁体   English

jQuery绑定-如何将此Ajax调用的结果绑定到文档?

[英]jQuery binding - how can I bind the result of this Ajax call to the document?

I am doing this: 我正在这样做:

jQuery("#country_id").change(function() {
    id = jQuery("#country_id").val();
    jQuery.ajax({
        type: "GET",
        url: 'index.php?option=com_jomdirectory&task=getprovince&format=raw&type=province',
        dataType: "html",
        data: "id=" + id,
        success: function(data) {
            jQuery('#provincelist').html(data).find('select').addClass("form-control chosen-select").chosen();
        }
    });
});

The problem is, this is not binding to the document, so when I try to fire the next ajax call (which retrieves a list of cities), 问题是,这没有绑定到文档,因此,当我尝试触发下一个ajax调用(检索城市列表)时,

I essentially have 3 selects: 我基本上有3个选择:

1) Country - onchange fires ajax call to get provinces (This works and gives me the Province select list) 1)国家/地区-onchange发出ajax呼叫以获取省份(此方法有效,并提供了省份选择列表)

2) Then when changing Province, it should fire an ajax call for city select 2)然后在更改省份时,应发出ajax调用以进行城市选择

3) City ajax call doesn't fire. 3)市ajax呼叫不会触发。

How do I go about binding the result to the document, so that I can fire the next ajax call on change, without fiddling with this piece of code too much? 我如何将结果绑定到文档,以便我可以在更改时触发下一个ajax调用,而又不会过多地干预这段代码?

Thanks, 谢谢,

J Ĵ

@PraveenKumar My next ajax call does not fire, because it is itself the result of an ajax call. @PraveenKumar我的下一个ajax调用不会触发,因为它本身是ajax调用的结果。 I essentially have 3 selects: 1) Country->Province->City and it works when I change country (the province select shows) but when I change province, nothing happens - the ajax call for city does not fire. 我基本上有3个选择:1)Country-> Province-> City,当我更改国家(选择的省份显示)时它起作用,但是当我更改省份时,什么也没发生-要求城市的Ajax调用不会触发。

you need to use event delegation for your province select. 您需要为您选择的省份使用事件委派。

jQuery("#provincelist").on('change', '#province_id', function() {

when you are replacing the select using jQuery('#provincelist').html(data) you are losing the change event on the province select as you are replacing the whole element. 当您使用jQuery('#provincelist').html(data)替换选择时,由于要替换整个元素,因此丢失了选择的更改事件。

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

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