单击单选按钮时的 Ajax 调用有时会在即时到约 2 秒之间交替

发布于 2024-09-08 17:18:00 字数 1237 浏览 2 评论 0原文

有 2 个单选按钮。根据您单击的数据,下拉框将加载该组数据。根据我单击的第一个,该加载大约需要 1-2 秒,而另一个则立即加载。甚至来回点击。我想知道为什么,以及如何让它立即停止加载(我知道很奇怪)。

$('#rblGenre').change( function() {
    $('#ddlSpecificGenre').attr('disabled','disabled').html( '<option>Loading...</option>' )
    var genre = $(this + ':checked').val();

    $.ajax({
        type: 'GET',
        url: '../bookGenres.xml',
        dataType: 'xml',
        success: function( xml ) {
            var xmlGenre = $(xml).find( genre );
            $('#ddlSpecificGenre').removeAttr('disabled').children().remove();
            xmlGenre.children().each( function() {
                $('#ddlSpecificGenre').append( '<option>' + $(this).text() + '</option>' );
            });
        }
    });
});

<asp:RadioButtonList ID='rblGenre' runat='server'>
    <asp:ListItem Text='Fiction' Value='Fiction'></asp:ListItem>
    <asp:ListItem Text='Non-Fiction' Value='Non-Fiction'></asp:ListItem>
</asp:RadioButtonList>

<asp:DropDownList ID='ddlSpecificGenre' runat='server' Enabled='false'>
    <asp:ListItem Text='Choose a Genre' Selected='True'></asp:ListItem>
</asp:DropDownList>

There are 2 radio buttons. Depending on which one you click, the drop down box will load that set of data. Depending on the first one I click, that one will take about 1-2 secs to load, while the other loads instantly. Even click back and forth. I was wondering why, and how I might be able to have it stop loading instantly (weird, I know).

$('#rblGenre').change( function() {
    $('#ddlSpecificGenre').attr('disabled','disabled').html( '<option>Loading...</option>' )
    var genre = $(this + ':checked').val();

    $.ajax({
        type: 'GET',
        url: '../bookGenres.xml',
        dataType: 'xml',
        success: function( xml ) {
            var xmlGenre = $(xml).find( genre );
            $('#ddlSpecificGenre').removeAttr('disabled').children().remove();
            xmlGenre.children().each( function() {
                $('#ddlSpecificGenre').append( '<option>' + $(this).text() + '</option>' );
            });
        }
    });
});

<asp:RadioButtonList ID='rblGenre' runat='server'>
    <asp:ListItem Text='Fiction' Value='Fiction'></asp:ListItem>
    <asp:ListItem Text='Non-Fiction' Value='Non-Fiction'></asp:ListItem>
</asp:RadioButtonList>

<asp:DropDownList ID='ddlSpecificGenre' runat='server' Enabled='false'>
    <asp:ListItem Text='Choose a Genre' Selected='True'></asp:ListItem>
</asp:DropDownList>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-09-15 17:18:00

在我看来,您的回复已被缓存。如果您想使用 jQuery 关闭缓存,请将“cache: false”添加到“ajax”调用中的设置列表中。

所以你的函数调用将变成:

$.ajax({
    type: 'GET',
    url: '../bookGenres.xml',
    dataType: 'xml',
    cache: false,
    success: function( xml ) {
        var xmlGenre = $(xml).find( genre );
        $('#ddlSpecificGenre').removeAttr('disabled').children().remove();
        xmlGenre.children().each( function() {
            $('#ddlSpecificGenre').append( '<option>' + $(this).text() + '</option>' );
        });
    }
});

Sounds to me like your response is getting cached. If you want to turn off caching using jQuery, add "cache: false" to your list of settings in your "ajax" call.

So your function call would become:

$.ajax({
    type: 'GET',
    url: '../bookGenres.xml',
    dataType: 'xml',
    cache: false,
    success: function( xml ) {
        var xmlGenre = $(xml).find( genre );
        $('#ddlSpecificGenre').removeAttr('disabled').children().remove();
        xmlGenre.children().each( function() {
            $('#ddlSpecificGenre').append( '<option>' + $(this).text() + '</option>' );
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文