2011-08-02 37 views
1

我創建了一個web.py URL,它只提供了2個文本框(無提交按鈕)。當我在第一個文本字段中鍵入數字時,它應該將該值發佈到另一個URL中。通過使用AJAX post()我想要將數據發佈到第二個URL。第二個網址應該會返回我發佈的數據,並且應該在第二個文本框中顯示。我怎樣才能解決這個問題,使用鍵盤功能和AJAX post()方法?我用post()方法嘗試了很多,但我找不到合適的解決方案。使用AJAX中的.post()方法將數據發佈到另一個URL

+2

它總是很好提供您正在嘗試的特定代碼。 – Andrea

+0

這個問題有點可疑。在尋求幫助之前嘗試寫十行JavaScript並不困難,尤其是jQuery [已有文檔](http://api.jquery.com/jQuery.post/)。 – Helgi

回答

1

試試這個

$(function(){ 

    $("#textbox1").keyup(function(){ 
    if($(this).val()){ 
     $.post({ 
     url: "urlToPost", 
     data: { "textbox1": $(this).val() }, 
     success: function(data){//make sure the server sends only the required data 
      $("#textbox2").val(data); 
     } 
     }); 
    } 
    }); 
}); 
+0

我嘗試了我的web.py GET函數中的代碼,但它會在第二個文本框中顯示結果。 – Sreehari

+0

您是否看到該頁面正在製作的帖子? – ShankarSangoli

+0

當你在文本框1中鍵入內容時,你是否看到從該頁面發出的任何請求。如果你正在使用FF或者在服務器端代碼中保留和斷點並檢查它是否被調用,請檢查Firebug控制檯或Net選項卡。 – ShankarSangoli

1

你可以發佈你有什麼這麼遠嗎?這似乎很straigth前進..

例如:

$(document).ready(function() { 
    //Bind to keyup + change to account for Copy/Paste with mouse button. 
    $("#Textbox1Id").bind("keyup change", function() { 
     $.ajax({ 
      url: 'yoururl', 
      type: 'POST', 
      data: { value: $("#Textbox1Id").val() }, 
      dataType: "json", 
      beforeSend: function() { 
      }, 
      success: function (result) { 
       $('#Textbox2Id').val(result.value); 
      }, 
      error: function (result) { 
       //Handle error 
      } 
     }); 
     return false; 
    }); 
}; 

尚卡爾打我給它...它基本上是相同的答案只是確保你綁定change事件還,並處理任何錯誤。

2

我找到了解決方案,感謝所有的建議,我已經附上了我的完整代碼和建議的更改。

import web 

urls = (
    '/', 'first','/home','second') 


app = web.application(urls, globals(), True) 


class first: 

    def GET(self): 
     return''' 
     <html> 
     <head> 
     <script type="text/javascript"src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script> 
     <script type="text/javascript"> 

     $(document).ready(function(){ 
      $("#text1").keyup(function(){ 
      var txt = $("#text1").val(); 
       $.post(
       "http://127.0.0.1:8080/home", 
       {text1 : txt}, 
       function(result){ 
       $("#text2").val(result); 
       }); 
      }); 
     }); 
     </script> 
     </head> 
     <body> 
     Enter your text here : <input type="text" id="text1" name="text1"/> 
     You entered   : <input type="text" id="text2"/> 
     </body> 
     </html>''' 

class second: 

    def GET(self): 
     return "Entered value goes here" 

    def POST(self): 
     i = web.input() 
     n = i.text1 
     return n 


if __name__ == "__main__": 
    app.run() 
相關問題