jquery mobile開發筆記之Ajax提交數據

這兩天學習了下,jquery mobile(如下簡稱jqm)的開發相關的內容。可能以前有過web的開發基礎,相對於我來講學習這個東西感受挺簡單的,很容易上手。Jqm的的語法和jquery實際上是同樣的,有些不大同樣的就是了。網上介紹的也是一大堆。這裏我主要是作筆記哈。php

    使用JQM開發其實很簡單,我這裏目前是針對於在服務器端開發的,服務器使用的是apache+php,前端其實主要是html5+jquery的寫法。css

 一、首先咱們能夠到官網去下載http://jquerymobile.com/blog/2012/09/21/jquery-mobile-1-2-0-release-candidate-2/#download   query mobile[/url],而後下載完成後,咱們能夠看到以下的的目錄結構   html

   jqm的包裏已經包含了demo和核心代碼。jqm提供的demo很全面,直接學習它基本就夠了。 既然下載好了,咱們就能夠進行開發了,概念的東西我就很少說了,直接上代碼。前端

 

二、編寫form表單頁面。html5

 

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>表單</title> 
    <!--加載jqm css樣式--> 
    <link rel="stylesheet"  href="css/jquery.mobile-1.2.0-rc.2.css" /> 
    <!--加載jquery--> 
    <script src="js/jquery.js"></script> 
    <!--加載jquery mobile--> 
    <script src="js/jquery.mobile-1.2.0-rc.2.js"></script>  
    <script src="js/ajax.js"></script>  
</head>  
<body> 
     
    <div data-role="page" data-fullscreen="true"><!--data-fullscreen 設置全屏--> 
 
    <div data-role="header"   data-position="inline"><!--data-position="inline" 設置以流的方式顯示--> 
        <a href="index.html" data-icon="delete">Cacel</a>  
        <h1>表單demo</h1> 
    </div><!-- /header --> 
 
    <div data-role="content">    
        <form id="ajaxForm"> 
            <div data-role="fieldcontain"> 
                <label for="username">User Name:</label> 
                <input type="text" name="username" id="username" data-mini="true"/> 
             
                <h3 id="notification"></h3> 
                <button data-theme="b" id="submit" type="submit">Submit</button> 
            </div> 
        </form>  
    </div><!-- /content --> 
 
    <div data-role="footer"  data-position="fixed"> 
        <h4>Page Footer</h4> 
    </div><!-- /footer --> 
</div><!-- /page --> 
 
</body> 
</html>

三、編寫服務器端腳本form.php(這裏我使用php)java

<?php 
    $username = $_POST['username']; 
    echo "User Name:".$username; 
?>

四、編寫ajax腳本ajax.js

$(function() { 
    $('#submit').bind('click', function() { 
 
        var formData = $('#ajaxForm').serialize(); 
        //.serialize() 方法建立以標準 URL 編碼表示的文本字符串 
      
        $.ajax({ 
            type : "POST", 
            url  : "form.php",  
            cache : false, 
            data : formData, 
            success : onSuccess, 
            error : onError 
        }); 
        return false; 
    }); 
}); 
 
function onSuccess(data,status){ 
    data = $.trim(data); //去掉先後空格 
    $('#notification').text(data); 
} 
 
function onError(data,status){ 
    //進行錯誤處理 
}

四、建立android的工程,使用webview進行訪問。jquery

layout:android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <WebView 
        android:id="@+id/webview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"  /> 
 
</RelativeLayout>

java代碼:

package com.xzw.html; 
 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
/** 
 *  
 * @author xuzw13@gmail.com 
 * weibo:http://weibo.com/xzw1989 
 * 
 */ 
public class MainActivity extends Activity { 
     
    private static final String TAG = "MainActivity";  
    private WebView webView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_main); 
         
        webView = (WebView)findViewById(R.id.webview); 
        webView.getSettings().setSupportZoom(true); 
        webView.getSettings().supportMultipleWindows(); 
        webView.getSettings().setJavaScriptEnabled(true); 
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
        webView.loadUrl("http://192.168.1.120/jquerymobile/index.html"); 
          
         
        webView.setWebChromeClient(new WebChromeClient(){ 
            @Override 
            public void onProgressChanged(WebView view, int newProgress) { 
                super.onProgressChanged(view, newProgress); 
            } 
        }); 
        webView.setWebViewClient(new MyWebViewClient()); 
         
         
    } 
     
    private class MyWebViewClient extends WebViewClient{ 
        @Override 
        public void onLoadResource(WebView view, String url) { 
          
                Log.i(TAG, "onLoadResource:" + url); 
          
            super.onLoadResource(view, url);  
        } 
         
        @Override 
        public void onReceivedError(WebView view, int errorCode, 
                String description, String failingUrl) { 
            Log.i(TAG, "onReceivedError:" + failingUrl+" \n errorcode="+errorCode); 
            super.onReceivedError(view, errorCode, description, failingUrl); 
        } 
         
          @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
                Log.i(TAG, "shouldOverrideUrlLoading:" + url);  
            view.loadUrl(url); 
            return true; 
        } 
           
          @Override 
        public void onPageStarted(WebView view, String url, Bitmap favicon) { 
                Log.i(TAG, "onPageStarted:" + url);  
        } 
           
          @Override 
        public void onPageFinished(WebView view, String url) { 
              Log.i(TAG, "onPageFinished:" + url);  
             
        } 
    }; 
     
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
         if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){ 
             webView.goBack(); 
             return true; 
         } 
        return super.onKeyDown(keyCode, event); 
         
    } 
} 
代碼就是所有代碼了。

代碼就是所有代碼了。
    
    補充下:jquery mobile的官網都是E文的,E文不是很好的童鞋直接學習jquery mobile的開發可能有些概念的東西沒有辦法很清楚地理解,我在學習的過程當中也遇到了這個問題。不過網上也有個中文版的,可是感受沒有E文版的感受舒服,不過也很不錯,你們學習的時候能夠兩個一塊兒對照。     
    附上兩個地址:

[url= http://jquerymobile.com/demos/1.1.1/ ] http://jquerymobile.com/demos/1.1.1/ [/url]

http://www.jqmapi.com/download.html

    歡迎你們一塊兒交流學習!請你們多多指教。web

相關文章
相關標籤/搜索