以往使用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
2 Responses to “[小技巧] jQuery serialize”
台湾兄弟 你这样写有问题吧
$(”input[@type=text]“).serialize();
$(":text").serialize();
(也可能是版本问题)
提交於 2009/04/14 12:43pm
嗯…是版本的問題 jquery 1.3 之後 就不能這樣寫了
當初寫的時候版本是1.2x…
不過…還是感謝告知 已經做修改了