[小技巧] 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

相關文章