国外设计欣赏网站 - DOOOOR.com

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,微信登陆

帖子

[Drupal问题] Drupal:自己定义一个表单,然后用ajax提交---jQuery.post的用法

[复制链接]
发表于 1-29-2012 16:26 | 显示全部楼层 |阅读模式
需 求
+ ~! Z6 a1 `/ W6 g& h在Drupal中自己定义一个表单,然后用ajax提交。

) V) `, j6 ~2 i( q/ |
解决方案
用html写一个form, 然后在form外面设置一个Button, 在button的onclick事件中调用jQuery.post()函数,将表单进行提交。
- u7 M. L7 I: W% ]6 A3 b2 S/ k) U有下面两点需要注意:
  • button一定要放在表单外面,否则就会刷新页面了
  • 每个input的控件必须有name, 如果没有name是无法获得数据的
  • 在用jQuery.post()进行提交时,需要将form序列化,使用jQuery的Helper的.serialize()即可! ?- S; D# Z) C9 ]2 F2 z5 @
示例代码3 O/ V- l+ J; B# x* p# q
<form id="form_charge"><label for="txt_name"> Name:</label> <input type="text" name="txt_name"  id="txt_name"></input><label for="txt_age"> Age:</label> <input type="text" name="txt_age" id="txt_age"></input></form><button onclick="$.post('/post/form', $('#form_charge').serialize(), function(data){$('#box_charge_result').html(data);});">Go!</button>
( t! t) ^7 P+ A. T& _0 W. l' n* c/ R
8 a  ~, H# _5 z1 d- _% p5 s这样在提交后,在/post/form路径中的$_POST会获得一个提交数组,如下面所示:4 }+ M( b0 I% _2 g
array(2) { ["txt_name"]=>  string(1) "Blade" ["txt_age"]=>  string(1) "100" }
; z2 a: ~- B# n$ A+ d$ @- W6 Q7 m1 b) o1 e/ y3 T

|2011-2026-版权声明|平台(网站)公约|DOOOOR 设计网 ( 吉ICP备2022003869号 )

GMT+8, 4-30-2025 20:37 , Processed in 0.774387 second(s), 474 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表