2011-03-20 52 views
0

我試圖創建訂閱的形式,基本上讓您選擇一個遊戲,然後負載是一堆認購價爲那場比賽的。到目前爲止,一切都很完美。問題是,當我嘗試輸入三個單選按鈕選擇payement 1個月/ 6月份/ 1年,我與阿賈克斯的onchange =「呼」的下拉似乎加載兩次的功能,並且內容不訪問。爲什麼我的ajax加載兩次相同的功能?

第一次,它完美的作品,它Echo的信息,並計算了一切。但第二次,它只是給了我一個PHP錯誤。

我已經想通了會發生什麼,它的CALL函數一次,與要求的訂閱價格ID(警報(planId)),以及它的回聲是完全因爲我想它。然後它只是再次調用該函數,但警報(planId);返回我未定義,並回聲一個新行(這是爲了發生)與錯誤代碼,說SQL請求失敗,因爲78線。

這使我回到我的問題,爲什麼AJAX調用函數兩次?

var xhr = { 
    planData:function(){ 
     if (httpRequest.readyState==4) { 
      if (httpRequest.status==200) { 
       document.getElementById('plan_select').innerHTML = 'ready'; 
       var price = httpRequest.responseText; 
       //alert(price); 
       var six = 6*parseInt(price); 
       var year = 12*parseInt(price); 
       if (document.getElementById('diff')) { 
        var ctnt = ''; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="one" />&nbsp;'; 
        ctnt += 'Month to month subscription plan ($'+price+' USD per month) &nbsp;'; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="six" />&nbsp;'; 
        ctnt += 'Six months subscription plan ($'+six+' USD per 6 months) &nbsp;'; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="year" />&nbsp;'; 
        ctnt += 'One year subscription plan ($'+year+' USD per year)'; 
        document.getElementById('diff').innerHTML = ctnt; 
       } else { 
        var yearPrices = document.getElementById('order').insertRow(3); 
        var cell = yearPrices.insertCell(0); 
        cell.id='diff'; 
        cell.colSpan = 3; 
        var ctnt = ''; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="one" />&nbsp;'; 
        ctnt += 'Month to month subscription plan ($'+price+' USD per month) &nbsp;'; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="six" />&nbsp;'; 
        ctnt += 'Six months subscription plan ($'+six+' USD per 6 months) &nbsp;'; 
        ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="year" />&nbsp;'; 
        ctnt += 'One year subscription plan ($'+year+' USD per year)'; 
        cell.innerHTML = ctnt; 
       } 
      } 
     } 
    }, 
    updatePrice:function(planId){ 
     alert(planId); 
     xhr.ajaxCall('GET', 'requests.php?action=getprices&planId='+planId, null, true, xhr.planData); 
    }, 
    ajaxCall:function(method,url,postData,async,handler) { 
     httpRequest = new XMLHttpRequest(); 
     httpRequest.open(method,url,async); 
     if (postData != null) 
      httpRequest.setRequestHeader("Content-Type","x-www-form-urlencoded"); 
     if (async) 
      httpRequest.onreadystatechange = handler; 
     httpRequest.send(postData); 
    } 
}; 

這是代碼的一部分,你會看到警報(planId);在updatePrice函數中。

下面是實際工作的事情鏈接:http://www.nitroflox.com/temp/

+1

你有任何的代碼嗎?這將會是非常方便的看到你的JavaScript這裏有一個很難 – Dexter 2011-03-20 17:38:12

+0

IM搞清楚如何來發表回覆 – nitroflox 2011-03-20 17:41:26

+0

你必須與你的代碼有問題,你甚至提一定行,但你不張貼代碼。你希望我們幫助你嗎?請編輯您的問題並添加代碼。 – 2011-03-20 17:42:24

回答

4

我只是看着你提供的鏈接。您兩次調用updatePrice功能:

onchange="xhr.updatePrice(xhr.updatePrice(this.options[this.selectedIndex].value)) 

更改爲:

onchange="xhr.updatePrice(this.options[this.selectedIndex].value) 

讓我知道,如果它的工作原理。

+0

哦,我的,/ facepalm它的工作,我會更多的下一次閱讀代碼。再次感謝。 – nitroflox 2011-03-20 18:16:10

相關問題