[小技巧] jQuery serialize

以往使用ajax傳值給php時,都需要將每一個 input 的值抓出來寫成 ‘xxx=xxx&xxx=xxx’, 現在jQuery 的 serialize 可以很簡單的達成,以下使用例子說明

<input type=’text’ name=’shian‘ id=’shian’ value=’shian‘/>

<input type=’text’ name=’john‘ id=’john’ value=’John‘/>

<input type=’text’ name=’boston‘ id=’boston’ value=’Boston‘/>

使用:

利用jQuery的serialize來取得所有 input的type=’text’值

$("input[@type=text]").serialize(); <= 此為 1.3版本以前的寫法

$("input[type=text]").serialize(); <= 1.3版本後的寫法

結果:

shian=shian&john=john&boston=Bostonn

Related Posts with Thumbnails

相關文章

2 thoughts on “[小技巧] jQuery serialize

    • 提交於 2009/04/14 12:43pm

      嗯…是版本的問題 jquery 1.3 之後 就不能這樣寫了

      當初寫的時候版本是1.2x…

      不過…還是感謝告知 已經做修改了

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>