簡體   English   中英

在javascript中調用單個ajax函數時,如何停止其他ajax函數自動執行?

[英]How to stop other ajax functions executing automatically while calling single ajax function in javascript?

我編寫了ajax函數,以便單擊時從服務器獲取數據。

1)瀏覽->單擊->我們得到年份。 [工作中]

2)年->單擊->我們得到月。 [工作中]

3)月->單擊->我們得到該月的星期。[工作]

這三個函數均從服務器獲取數據。

但是當我點擊“年份”獲得幾個月時,

2個功能被觸發

1)獲得我點擊的月份。

2)獲取年份,僅通過瀏覽鏈接調用(該函數自動調用)。 因此,在獲取月份函數執行后再次調用獲取年份函數。

同樣,我有第三個函數返回該月的星期。 當我單擊任何月份以獲取星期時,將調用3個函數

1)為了獲得一周-我在點擊時致電給我。

2)要獲得月-自動調用。

3)獲取年-自動調用。

我在下面列出的2個功能

1)getYear 2)getMonth

函數getYears()

function getYears() {

        var ul = $('.panel-body ul');
        if (ul.length > 0) {
            ul.html('');
        }
        var liopen = '<li class="list-group-item"><a href="#" onclick="getMonths(this);">';
        var liclose = '</a></li>';

        //showing progress bar
        progress.fadeIn(500);

        $.ajax({
            type: "POST",
            url: "Test.aspx/loadYears",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                progress.fadeOut(500);
                var obj = JSON.parse(data.d);
                console.log(obj);
                for (var i = 0; i < obj.length; i++) {
                    ul.append(liopen + obj[i] + liclose);


               }

            },
            error:function(errorData)
            {
                console.log(errorData.status);
            }
        });
    }

函數getMonths(target)

function getMonths(target) {


        progress.fadeIn(500);
        var targetLink = $(target);
        var ifData = $(target).find('ul');
        if (ifData.length > 0) {
            ifData.html('');
        }
        var ul = $(document.createElement('ul'));

        var liopen = '<li><a href="#" onclick="getWeeks(this);">';
        var liclose = '</a></li>';
        var year = parseInt( $(target).text());
        $.ajax({
            type: "POST",
            url: "Test.aspx/loadMonths",
            data:'{year_ : "' + year + '"}',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                progress.fadeOut(500);
                var obj = JSON.parse(data.d);
                console.log(obj);
                for (var i = 0; i < obj.length; i++) {
                    ul.append(liopen + obj[i] + liclose);


                }
                targetLink.append(ul);
                return true;
            },
            error: function (errorData) {
                console.log(errorData.status);
            }
        });
    }

瀏覽

<a href="#" style="z-index:3;color:grey" onclick="getYears();">Browse Site reports</a>

<ul class="list-group"><li class="list-group-item"><a href="#" onclick="getMonths(this);">2016</a></li></ul>

<ul><li><a href="#" onclick="getWeeks(this);">Jul</a></li></ul>

問題是所有數據都附加到“ a”標簽本身。 因此,如果單擊嵌套在主標簽本身中的getMonths函數,則也要單擊該標簽。 像下面的代碼:

<a click=func1();>
  <ul>
    <li>
      <a click=function2();>
      </a>
   </li>
  </ul>
</a>

問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM